Jackson转换器和Javax验证(注释)不能一起工作



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

<!-- 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声明优先)

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

非常感谢您的帮助。春季版本4.0.6

我选择了验证工作的部分,并将其添加到代码库中。

@RequestMapping(value = "url", method = RequestMethod.GET)
protected void getLocationAsJson(@PathVariable("id")   Integer id,
 @RequestParam("cid") Integer cid, HttpServletResponse response) {
    MappingJacksonHttpMessageConverter jsonConverter = 
            new MappingJacksonHttpMessageConverter();
    Location loc= new Location(id);
    MediaType jsonMimeType = MediaType.APPLICATION_JSON;
    if (jsonConverter.canWrite(requestedLocation.getClass(), jsonMimeType)) {
    try {
        jsonConverter.write(requestedLocation, jsonMimeType,
                               new ServletServerHttpResponse(response));
        } catch (IOException m_Ioe) {
            // TODO: announce this exception somehow
        } catch (HttpMessageNotWritableException p_Nwe) {
            // TODO: announce this exception somehow
        }
    }
}

现在验证和JSON返回一样有效。该方法没有返回任何内容。

RequestMappingHandlerAdapter的xml配置有点复杂。此配置的问题在于,它删除了转换器的弹簧默认配置。最好使用此配置的编码版本。通过这种方式,Spring默认配置将保持不变。以下是配置示例。

建议的解决方案,发布在许多博客上。但对我来说不起作用

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

最新更新