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

查看:86
本文介绍了杰克逊转换器和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 version 4.0.6

Any help greatly appreciated. Spring version 4.0.6

推荐答案

RequestMappingHandlerAdapter 's 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 / customizing
http ://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);
    }
}






工作解决方案




Working Solution

@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());
}

}

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

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