Make JSON payload fields case insensitive when mapping to Java Object in REST API developed using SpringBoot

后端 未结 2 1723
自闭症患者
自闭症患者 2020-12-15 10:07

I am working on REST API developed using SpringBoot application. Here I want to make the fields in the payload(JSON) as case insensitive when mapping to a Java Object. Below

相关标签:
2条回答
  • 2020-12-15 10:45

    Well, if its Spring Boot application you can have this in your application.properties file: spring.jackson.mapper.accept_case_insensitive_properties=true

    or if you use yaml:

    spring:
      jackson:
        mapper:
          accept_case_insensitive_properties: true
    
    0 讨论(0)
  • 2020-12-15 10:47

    I recently found a solution via 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;
        }
    
    }
    

    I'm using these dependencies:

    <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>
    

    Good luck.

    0 讨论(0)
提交回复
热议问题