2014-10-10 32 views
1

如果我用下面的配置,然后杰克逊转换器作品(MVC声明是最后一次)杰克逊转换器和使用javax验证(注释)不能一起工作

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

回答

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返回。 该方法没有返回任何东西。

0

RequestMappingHandlerAdapter的xml配置有点复杂。这个配置的问题是,它删除了转换器的弹簧默认配置。最好使用此配置的编码版本。 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()); 
} 

}