Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a different ObjectMapper for certain package Spring MVC

Is there a way to set a HttpMessageConverter for only certain packages in my project using spring?

@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = MyConfig.class)
public class MyConfig
    extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        for (HttpMessageConverter<?> converter : converters) {
            if (converter instanceof MappingJacksonHttpMessageConverter) {
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
                objectMapper.setSerializationInclusion(Inclusion.NON_NULL);
                ((MappingJacksonHttpMessageConverter) converter).setObjectMapper(objectMapper);
            }
        }
    }

Using this PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES, changes all responses in project and I want this to apply only in some dto.

like image 497
nachokk Avatar asked Aug 10 '15 17:08

nachokk


1 Answers

Here an example of the solution propose by hzpz :

Custom converter :

public class CustomConverter extends MappingJackson2HttpMessageConverter {

    private String basePackage;

    public CustomConverter(String basePackage) {
        this.basePackage = basePackage;
    }

    @Override
    public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
        if (contextClass.getName().startsWith(basePackage)) {
            return super.canRead(type, contextClass, mediaType);
        }
        return false;
    }

    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        if (clazz.getName().startsWith(basePackage)) {
            return super.canWrite(clazz, mediaType);
        }
        return false;
    }
}

Configuration

@Configuration
public class CustomConfig {

    @Bean
    public MappingJackson2HttpMessageConverter firstConverter() {
        MappingJackson2HttpMessageConverter converter = new CustomConverter("package.camel");
        converter.setObjectMapper(firstObjectMapper());
        return converter;
    }

    @Bean
    public MappingJackson2HttpMessageConverter secondConverter() {
        MappingJackson2HttpMessageConverter converter = new CustomConverter("package.snake");
        converter.setObjectMapper(secondObjectMapper());
        return converter;
    }

    @Bean
    public ObjectMapper firstObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
        return mapper;
    }

    @Bean
    public ObjectMapper secondObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        return mapper;
    }
}
like image 94
Jean-Philippe Bond Avatar answered Nov 15 '22 01:11

Jean-Philippe Bond