程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在使用GsonHttpMessageConverter之前在Spring中配置Gson大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在使用GsonhttpmessageConverter之前在Spring中配置Gson?

开发过程中遇到在使用GsonhttpmessageConverter之前在Spring中配置Gson的问题如何解决?下面主要结合日常开发的经验,给出你关于在使用GsonhttpmessageConverter之前在Spring中配置Gson的解决方法建议,希望对你解决在使用GsonhttpmessageConverter之前在Spring中配置Gson有所启发或帮助;

(新方法)使用Java Config

扩展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@

您可以阅读有关如何自定义提供的配置的更多信息。@H_673_9@

(旧方法)使用XML配置

在dispatcherServlet上下文中:@H_673_9@

<?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@

解决方法

在构造GsonhttpmessageConverter之前如何配置Gson?@H_673_9@

我需要使用@Expose并指定日期格式。@H_673_9@

大佬总结

以上是大佬教程为你收集整理的在使用GsonHttpMessageConverter之前在Spring中配置Gson全部内容,希望文章能够帮你解决在使用GsonHttpMessageConverter之前在Spring中配置Gson所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。