大佬教程收集整理的这篇文章主要介绍了在使用GsonHttpMessageConverter之前在Spring中配置Gson,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
扩展 您可以阅读有关如何自定义提供的配置的更多信息。@H_673_9@
在dispatcherServlet上下文中:@H_673_9@
以上是大佬教程为你收集整理的在使用GsonHttpMessageConverter之前在Spring中配置Gson全部内容,希望文章能够帮你解决在使用GsonHttpMessageConverter之前在Spring中配置Gson所遇到的程序开发问题。 如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。WebMvcConfigurerAdapter@H_607_6@或如果需要更多控件使用
WebMvcConfigurationSupport@H_607_6@。@H_673_9@
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@OverrIDe
public voID configuremessageConverters(List<httpmessageConverter<?>> converters) {
converters.add(createGsonhttpmessageConverter());
super.configuremessageConverters(converters);
}
private GsonhttpmessageConverter createGsonhttpmessageConverter() {
Gson gson = new GsonBuilder()
.excludeFIEldsWithoutExposeAnnotation()
.setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
.create();
GsonhttpmessageConverter gsonConverter = new GsonhttpmessageConverter();
gsonConverter.setGson(gson);
return gsonConverter;
}
}
@H_607_6@
(旧方法)使用XML配置
<?xml version="1.0" enCoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/scheR_119_11845@a/beans"
xmlns:mvc="http://www.springframework.org/scheR_119_11845@a/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLscheR_119_11845@a-instance"
xsi:scheR_119_11845@aLOCATIOn="http://www.springframework.org/scheR_119_11845@a/mvc
http://www.springframework.org/scheR_119_11845@a/mvc/spring-mvc.xsd
http://www.springframework.org/scheR_119_11845@a/beans
http://www.springframework.org/scheR_119_11845@a/beans/spring-beans.xsd">
<bean ID="gsonBuilder" class="com.Google.gson.GsonBuilder">
<property name="dateFormat" value="yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'" />
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingfactorybean">
<property name="targetobject" ref="gsonBuilder" />
<property name="targetmethod" value="excludeFIEldsWithoutExposeAnnotation" />
</bean>
<mvc:Annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.Json.GsonhttpmessageConverter">
<property name="gson">
<bean class="com.Google.gson.Gson" factory-bean="gsonBuilder" factory-method="create" />
</property>
</bean>
</mvc:message-converters>
</mvc:Annotation-driven>
</beans>
@H_607_6@
解决方法
大佬总结
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。