Java SpringMVC Jackson2HttpMessageConverter定制不';行不通
我想为SpringMVC4的JSON响应使用定制JsonSerializer 为了添加JsonSerializer,我创建了WebMVCConfigureAdapter子类。 但是MappingJackson2HttpMessageConverter的定制不起作用 为了简化问题,我尝试了setJsonPrefix。 但它也不起作用。反应没有改变 我的代码如下。请告诉我怎么了 控制器类Java SpringMVC Jackson2HttpMessageConverter定制不';行不通,java,spring,spring-mvc,jackson,Java,Spring,Spring Mvc,Jackson,我想为SpringMVC4的JSON响应使用定制JsonSerializer 为了添加JsonSerializer,我创建了WebMVCConfigureAdapter子类。 但是MappingJackson2HttpMessageConverter的定制不起作用 为了简化问题,我尝试了setJsonPrefix。 但它也不起作用。反应没有改变 我的代码如下。请告诉我怎么了 控制器类 @Controller public class SampleController { @Reques
@Controller
public class SampleController {
@RequestMapping("/sample")
@ResponseBody
public ResponseModel action() {
return new ResponseModel();
}
public static class ResponseModel {
public String id = "001";
public String text = "aaa";
}
}
配置
@Configuration
@EnableWebMvc
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(converter());
super.configureMessageConverters(converters);
}
@Bean
protected MappingJackson2HttpMessageConverter converter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setJsonPrefix("prefix");
return converter;
}
}
在每种情况下,我都没有错误。但是定制不起作用。在configureMessageConverters方法中,如果未涵盖您,并且未添加其他转换器,则转换器为空,WebMvcConfigurationSupport将调用addDefaultHttpMessageConverters,这将配置默认转换器,其中包含默认的MappingJackson2HttpMessageConverter 因此,在extendMessageConverters中添加MappingJackson2HttpMessageConverter将不起作用 有两种解决方案:
对不起,我说的是蹩脚的英语。我也遇到了这个问题,并发现了这个问题,这要感谢另一个网站: @EnableWebMvc相当于基于XML的配置
如果两者都有,那么extendMessageConverters似乎并不有效。一旦我删除了XML条目,宾果。。自定义转换器开始工作。您在哪里使用它,您可以粘贴应该修改的错误日志或响应。您是否尝试删除配置中的@EnableWebMvc?我尝试删除@EnableWebMvc。在这种情况下,我使用WebMvcConfigurationSupport而不是WebMVCConfigureAdapter。实际上,我将CustomObjectMapper设置为MappingJackson2HttpMessageConverter。上述代码的响应为{“id”:“001”,“text”:“aaa”}。我没有错。但是响应没有JsonPrefix。我使用JSONSerializer添加了代码。
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
xmlns:aop="http://www.springframework.org/schema/aop">
<!-- base package -->
<context:annotation-config />
<context:component-scan base-package="jp.co.xxx.*" /><!-- my package. contains WebMvcConfiguration class -->
<annotation-driven />
<!-- aop -->
<aop:aspectj-autoproxy />
</beans:beans>
@Configuration
@EnableWebMvc
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
@Autowired
protected CustomObjectMapper mapper;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(converter());
super.configureMessageConverters(converters);
}
@Bean
protected MappingJackson2HttpMessageConverter converter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(mapper);
return converter;
}
}
@Component
public class CustomObjectMapper extends ObjectMapper {
private static final long serialVersionUID = -6987863269632420904L;
public CustomObjectMapper() {
setSerializationInclusion(Include.NON_NULL);
enable(SerializationFeature.INDENT_OUTPUT);
SimpleModule module = new SimpleModule();
module.addSerializer(DateTime.class, new DateTimeSerializer());
registerModule(module);
}
}