복붙노트

[SPRING] SpringBoot를 사용하여 개발 된 REST API에서 Java Object로 매핑 할 때 JSON 페이로드 필드를 대소 문자를 구분하지 않도록한다.

SPRING

SpringBoot를 사용하여 개발 된 REST API에서 Java Object로 매핑 할 때 JSON 페이로드 필드를 대소 문자를 구분하지 않도록한다.

Spring API를 사용하여 개발 된 REST API를 개발 중이다. 어디에 자바 객체에 매핑 할 때 페이로드 (JSON)의 필드를 대소 문자를 구분하지 않고서 만들고 싶습니다. 아래는 제 코드입니다.

유효 탑재량 { "limit": 1, "name": "MATTHEW PHILLIPS"}

개체 요청

private String limit;
private String name;

public String getLimit() {
      return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

@RequestBody Spring annotation을 사용하여 JSON 페이로드를 Java Object에 직접 매핑한다. 내 질문은 여기 어떻게 사건을 무시하고 필드에 매핑하는 자바 개체를 만들 수 있습니다.

문안 인사, Rakesh

해결법

  1. ==============================

    1.글쎄, 스프링 부트 애플리케이션이라면 application.properties 파일에 다음과 같이 넣을 수있다. spring.jackson.mapper.accept_case_insensitive_properties = true

    글쎄, 스프링 부트 애플리케이션이라면 application.properties 파일에 다음과 같이 넣을 수있다. spring.jackson.mapper.accept_case_insensitive_properties = true

    또는 yaml을 사용하는 경우 :

    spring:
      jackson:
        mapper:
          accept_case_insensitive_properties: true
    
  2. ==============================

    2.대 / 소문자 구분을 무시하기 위해 사용자 지정 디시리얼라이저를 작성해야 할 수 있습니다.

    대 / 소문자 구분을 무시하기 위해 사용자 지정 디시리얼라이저를 작성해야 할 수 있습니다.

    이 질문에 대한 대답이 있습니까?

  3. ==============================

    3.최근 Annotation config를 통해 해결책을 찾았습니다.

    최근 Annotation config를 통해 해결책을 찾았습니다.

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import com.fasterxml.jackson.databind.MapperFeature;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    @Configuration
    public class Config {
    
        @Bean
        public ObjectMapper objectMapper() {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
            return mapper;
        }
    
    }
    

    이러한 종속성을 사용하고 있습니다.

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
    
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>2.6.5</version>
        </dependency>
    

    행운을 빕니다.

  4. from https://stackoverflow.com/questions/30924816/make-json-payload-fields-case-insensitive-when-mapping-to-java-object-in-rest-ap by cc-by-sa and MIT license