Spring mvc Spring MVC版本4.2.5.0明确要求应用程序/json使用HttpMessageConverter
弹簧4.2.5.1释放是否需要添加Spring mvc Spring MVC版本4.2.5.0明确要求应用程序/json使用HttpMessageConverter,spring-mvc,Spring Mvc,弹簧4.2.5.1释放是否需要添加 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.jso
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
我使用的是旧版本的spring,不记得添加了这个。
我正在从3.2.7升级到4.2.5,发现了这个问题。以下HttpMessageConverters实例在Spring MVC的新版本中默认预先启用:
ByteArrayHttpMessageConverter – converts byte arrays
StringHttpMessageConverter – converts Strings
ResourceHttpMessageConverter – converts org.springframework.core.io.Resource for any type of octet stream
SourceHttpMessageConverter – converts javax.xml.transform.Source
FormHttpMessageConverter – converts form data to/from a MultiValueMap<String, String>.
Jaxb2RootElementHttpMessageConverter – converts Java objects to/from XML (added only if JAXB2 is present on the classpath)
MappingJackson2HttpMessageConverter – converts JSON (added only if Jackson 2 is present on the classpath)
MappingJacksonHttpMessageConverter – converts JSON (added only if Jackson is present on the classpath)
AtomFeedHttpMessageConverter – converts Atom feeds (added only if Rome is present on the classpath)
RssChannelHttpMessageConverter – converts RSS feeds (added only if Rome is present on the classpath)
ByteArrayHttpMessageConverter–转换字节数组
StringHttpMessageConverter–转换字符串
ResourceHttpMessageConverter–为任何类型的八位字节流转换org.springframework.core.io.Resource
SourceHttpMessageConverter–转换javax.xml.transform.Source
FormHttpMessageConverter–将表单数据与多值映射进行转换。
Jaxb2RootElementHttpMessageConverter–将Java对象转换为XML或从XML转换为Java对象(仅当类路径上存在JAXB2时添加)
MappingJackson2HttpMessageConverter–转换JSON(仅当类路径上存在Jackson 2时添加)
MappingJacksonHttpMessageConverter–转换JSON(仅当类路径上存在Jackson时添加)
AtomFeedHttpMessageConverter–转换Atom提要(仅在类路径上存在Rome时添加)
RssChannelHttpMessageConverter–转换RSS提要(仅当类路径上存在Rome时添加)
OP进一步询问我使用的是什么viewResolver。我认为向他展示我使用的所有消息转换器和viewResolver是合适的
@Inject ObjectMapper objectMapper;
@Inject Marshaller marshaller;
@Inject Unmarshaller unmarshaller;
@Inject SpringValidatorAdapter validator;
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters
) {
converters.add(new SourceHttpMessageConverter<>());
MarshallingHttpMessageConverter xmlConverter =
new MarshallingHttpMessageConverter();
xmlConverter.setSupportedMediaTypes(Arrays.asList(
new MediaType("application", "xml"),
new MediaType("text", "xml")
));
xmlConverter.setMarshaller(this.marshaller);
xmlConverter.setUnmarshaller(this.unmarshaller);
converters.add(xmlConverter);
MappingJackson2HttpMessageConverter jsonConverter =
new MappingJackson2HttpMessageConverter();
jsonConverter.setSupportedMediaTypes(Arrays.asList(
new MediaType("application", "json"),
new MediaType("text", "json")
));
jsonConverter.setObjectMapper(this.objectMapper);
converters.add(jsonConverter);
}
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer)
{
configurer.favorPathExtension(false).favorParameter(false)
.ignoreAcceptHeader(false)
.defaultContentType(MediaType.APPLICATION_JSON);
}
@Bean
public LocaleResolver localeResolver()
{
return new AcceptHeaderLocaleResolver();
}
}
@Bean
public ObjectMapper objectMapper()
{
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE,
false);
return mapper;
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller()
{
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan(new String[] { "PACKAGE_NAME" });
return marshaller;
}
@Inject ObjectMapper ObjectMapper;
@注入马歇尔勒;
@注入解组器解组器;
@注入SpringValidator适配器验证程序;
@凌驾
公共无效配置MessageConverters(
列表以下HttpMessageConverters实例在Spring MVC的新版本中默认预先启用:
ByteArrayHttpMessageConverter – converts byte arrays
StringHttpMessageConverter – converts Strings
ResourceHttpMessageConverter – converts org.springframework.core.io.Resource for any type of octet stream
SourceHttpMessageConverter – converts javax.xml.transform.Source
FormHttpMessageConverter – converts form data to/from a MultiValueMap<String, String>.
Jaxb2RootElementHttpMessageConverter – converts Java objects to/from XML (added only if JAXB2 is present on the classpath)
MappingJackson2HttpMessageConverter – converts JSON (added only if Jackson 2 is present on the classpath)
MappingJacksonHttpMessageConverter – converts JSON (added only if Jackson is present on the classpath)
AtomFeedHttpMessageConverter – converts Atom feeds (added only if Rome is present on the classpath)
RssChannelHttpMessageConverter – converts RSS feeds (added only if Rome is present on the classpath)
ByteArrayHttpMessageConverter–转换字节数组
StringHttpMessageConverter–转换字符串
ResourceHttpMessageConverter–为任何类型的八位字节流转换org.springframework.core.io.Resource
SourceHttpMessageConverter–转换javax.xml.transform.Source
FormHttpMessageConverter–将表单数据与多值映射进行转换。
Jaxb2RootElementHttpMessageConverter–将Java对象转换为XML或从XML转换为Java对象(仅当类路径上存在JAXB2时添加)
MappingJackson2HttpMessageConverter–转换JSON(仅当类路径上存在Jackson 2时添加)
MappingJacksonHttpMessageConverter–转换JSON(仅当类路径上存在Jackson时添加)
AtomFeedHttpMessageConverter–转换Atom提要(仅在类路径上存在Rome时添加)
RssChannelHttpMessageConverter–转换RSS提要(仅当类路径上存在Rome时添加)
OP进一步询问我使用的是什么viewResolver。我认为向他展示我使用的所有消息转换器和viewResolver是合适的
@Inject ObjectMapper objectMapper;
@Inject Marshaller marshaller;
@Inject Unmarshaller unmarshaller;
@Inject SpringValidatorAdapter validator;
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters
) {
converters.add(new SourceHttpMessageConverter<>());
MarshallingHttpMessageConverter xmlConverter =
new MarshallingHttpMessageConverter();
xmlConverter.setSupportedMediaTypes(Arrays.asList(
new MediaType("application", "xml"),
new MediaType("text", "xml")
));
xmlConverter.setMarshaller(this.marshaller);
xmlConverter.setUnmarshaller(this.unmarshaller);
converters.add(xmlConverter);
MappingJackson2HttpMessageConverter jsonConverter =
new MappingJackson2HttpMessageConverter();
jsonConverter.setSupportedMediaTypes(Arrays.asList(
new MediaType("application", "json"),
new MediaType("text", "json")
));
jsonConverter.setObjectMapper(this.objectMapper);
converters.add(jsonConverter);
}
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer)
{
configurer.favorPathExtension(false).favorParameter(false)
.ignoreAcceptHeader(false)
.defaultContentType(MediaType.APPLICATION_JSON);
}
@Bean
public LocaleResolver localeResolver()
{
return new AcceptHeaderLocaleResolver();
}
}
@Bean
public ObjectMapper objectMapper()
{
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE,
false);
return mapper;
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller()
{
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan(new String[] { "PACKAGE_NAME" });
return marshaller;
}
@Inject ObjectMapper ObjectMapper;
@注入马歇尔勒;
@注入解组器解组器;
@注入SpringValidator适配器验证程序;
@凌驾
公共无效配置MessageConverters(
列表我在我的依赖项中添加了Jackson2,但它如何不使用MappingJackson2HttpMessageConverter。您能告诉我您正在使用的视图解析程序是什么吗?我在我的依赖项中添加了Jackson2,但如何不使用MappingJackson2HttpMessageConverter。您能告诉我您正在使用的视图解析程序是什么吗真的不需要。向我们展示您的上下文配置的其余部分。我想我的配置中缺少了。添加它后解决了我的问题。@Faraz感谢所有的描述。但我一直在寻找xml配置。我后来发现了。@VijendraKulhade哦,好的。感谢您接受我的答案。您真的不需要。展示告诉我们您的上下文配置的其余部分。我想我的配置中缺少了它。添加它后解决了我的问题。@Faraz感谢您的所有描述。但我正在寻找xml配置。我后来发现了它。@VijendraKulhade哦,好的。感谢您接受我的回答。