Jackson 转换器和 Javax 验证(注释)不能一起工作 [英] Jackson converter and Javax Validation (annotation) not working together

查看:27
本文介绍了Jackson 转换器和 Javax 验证(注释)不能一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用以下配置,那么jackson转换器就可以工作(mvc声明在最后)

If I use the following configuration then jackson converter works (mvc declaration is last)

<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter"      class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
<context:component-scan base-package="com.base" />
<mvc:annotation-driven />

如果我在 dispatcher.xml 中使用此配置,则验证有效但转换无效.(mvc 声明先)

If I use this configuration in dispatcher.xml then validation works but conversion does not. (mvc declaration first)

<context:component-scan base-package="com.base" />

<mvc:annotation-driven />

<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>

非常感谢任何帮助.Spring 4.0.6 版

Any help greatly appreciated. Spring version 4.0.6

推荐答案

RequestMappingHandlerAdapter 的 xml 配置有点复杂.此配置的问题在于,它删除了转换器的 spring 默认配置.最好使用此配置的编码版本.Spring 默认配置将保持不变.这是示例配置.

RequestMappingHandlerAdapter's xml configuration is bit complicated. The problem with this configuration is, it removes spring default configuration for converters. It is better to use coding version of this configuration. Spring default configuration will be intact this way. Here is sample configurations.

https://dzone.com/articles/customizinghttp://www.java-allandsundry.com/2014/09/customizing-httpmessageconverters-with.html

@Configuration
public class MessageConvertorConfiguration extends WebMvcConfigurationSupport {
    @Bean
    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        Custom360DateFormat dateFormat = new Custom360DateFormat();
        dateFormat.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
        dateFormat.setDateTimeFormat(new SimpleDateFormat("MM/dd/yyyy hh:mm a"));

        objectMapper.setDateFormat(dateFormat);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jsonConverter.setObjectMapper(objectMapper);
        return jsonConverter;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(customJackson2HttpMessageConverter());
        super.addDefaultHttpMessageConverters(converters);
    }
}

<小时>

工作解决方案

@Configuration
public class MessageConvertorConfiguration  {

    private MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        Custom360DateFormat dateFormat = new Custom360DateFormat();
        dateFormat.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
        dateFormat.setDateTimeFormat(new SimpleDateFormat("MM/dd/yyyy hh:mm a"));

        objectMapper.setDateFormat(dateFormat);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jsonConverter.setObjectMapper(objectMapper);
        return jsonConverter;
    }

   @Autowired
public void updateJacksonConvertor(RequestMappingHandlerAdapter handlerAdapter) {

    //remove default jakson convertor
    Iterator<HttpMessageConverter<?>> convertorsIterator = handlerAdapter.getMessageConverters().iterator();
    while (convertorsIterator.hasNext()) {
        HttpMessageConverter converter = convertorsIterator.next();
        if(converter instanceof AbstractJackson2HttpMessageConverter) {
            convertorsIterator.remove();
        }
    }

    handlerAdapter.getMessageConverters().add(customJackson2HttpMessageConverter());
}

}

这篇关于Jackson 转换器和 Javax 验证(注释)不能一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆