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

后端 未结 2 1724
自闭症患者
自闭症患者 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: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:

    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.3.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    
        
            org.springframework.boot
            spring-boot-starter-web
        
    
        
            org.springframework
            spring-core
        
    
        
            com.fasterxml.jackson.module
            jackson-module-jaxb-annotations
            2.6.5
        
    

    Good luck.

提交回复
热议问题