杰克逊转换器和Javax验证(注释)没有合作

时间:2014-10-10 00:15:22

标签: json validation spring-mvc annotations jackson

如果我使用以下配置,那么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>

任何帮助非常感谢。 Spring版本4.0.6

2 个答案:

答案 0 :(得分:0)

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

@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返回。 该方法没有返回任何内容。

答案 1 :(得分:0)

RequestMappingHandlerAdapter的xml配置有点复杂。此配置的问题是,它删除了转换器的spring默认配置。最好使用此配置的编码版本。 Spring默认配置将以这种方式完整。以下是示例配置。

建议的解决方案,发布在众多博客上。但不适用于我的情况。

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

工作解决方案

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

}