问题描述
我想将自定义JsonSerializer用于SpringMVC4的JSON响应。
I want to use custom JsonSerializer for JSON response of SpringMVC4.
为了添加JsonSerializer,我创建了WebMvcConfigurerAdapter子类。
但是MappingJackson2HttpMessageConverter的自定义不起作用。
In order to add JsonSerializer, I created WebMvcConfigurerAdapter subclass.But customization of MappingJackson2HttpMessageConverter didn't work.
简化问题,我尝试了setJsonPrefix。
但它也没有用。答案没有改变。
Simplify the problem, I tried setJsonPrefix.But it also didn't work. The response didn't changed.
我的代码如下。请告诉我有什么问题。
My code is below. Please tell me what is wrong.
ControllerClass
@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;
}
}
dispatcher-servlet.xml
<?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>
注意。
-
当服务器启动时,调用了configureMessageConverters方法。
(断点确认)
When server starts, configureMessageConverters method was called.(Breakpoint confirmation)
我正在为JSONP
使用AbstractJsonpResponseBodyAdvice子类(我删除了这个类,但没有更改。)
I am using AbstractJsonpResponseBodyAdvice subclass for JSONP(I removed this class, but nothing was changed.)
我在下面用作参考。
- SpringMVC版本为4.1.6
PS
在JSONSerializer案例中如下。
P.S.In JSONSerializer case is below.
@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;
}
}
ObjectMapper
@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);
}
}
在每种情况下我都没有错误。但是自定义不起作用。
In each case I had no error. But customization didn't work.
推荐答案
在configureMessageConverters方法中,如果没有覆盖并且没有添加其他转换器,则转换器是空的,WebMvcConfigurationSupport将调用addDefaultHttpMessageConverters,它将配置默认转换器,其中包含默认的MappingJackson2HttpMessageConverter。
In the configureMessageConverters method, if you are not covered and no other converter is added, converters are empty, and WebMvcConfigurationSupport will call addDefaultHttpMessageConverters, which will configure the default converter, which contains the default MappingJackson2HttpMessageConverter.
因此在extendMessageConverters中添加MappingJackson2HttpMessageConverter将无效。
So adding MappingJackson2HttpMessageConverter in extendMessageConverters will not work.
有两种解决方案:
-
在configureMessageConverters方法中添加所需的转换器本身
You add the required converter in the configureMessageConverters method itself
要确定extendMessageConverters中的转换器类型,请设置所需的属性
To determine the type of converter in extendMessageConverters, set the required properties
对不起,我说英语坏了。
sorry,i speek broken english.
这篇关于SpringMVC Jackson2HttpMessageConverter定制不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!