在使用GsonHttpMessageConverter之前,在Spring中配置Gson [英] Configure Gson in Spring before using GsonHttpMessageConverter

查看:235
本文介绍了在使用GsonHttpMessageConverter之前,在Spring中配置Gson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何配置 Gson 构建 GsonHttpMessageConverter



我需要使用 @ Expose 并指定日期格式。 (新方法)使用Java配置

扩展 WebMvcConfigurerAdapter 或者如果您需要更多控制,请使用 WebMvcConfigurationSupport

  @Configuration 
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Override
public void configureMessageConverters(List< HttpMessageConverter<>>转换器){
converters.add(createGsonHttpMessageConverter());
super.configureMessageConverters(转换器);

$ b $ 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);

返回gsonConverter;
}

}

您可以阅读更多关于如何自定义提供的配置
$ b

(旧方法)使用XML配置



在DispatcherServlet上下文中:

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:mvc =http://www.springframework.org/schema/mvc
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
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.xsd>

< bean id =gsonBuilderclass =com.google.gson.GsonBuilder>
< property name =dateFormatvalue =yyyy' - 'MM' - 'dd'T'HH':'mm':'ss'。'SSS'Z'/>
< / bean>

< bean class =org.springframework.beans.factory.config.MethodInvokingFactoryBean>
< property name =targetObjectref =gsonBuilder/>
< property name =targetMethodvalue =excludeFieldsWithoutExposeAnnotation/>
< / bean>

< mvc:注释驱动>
< mvc:message-converters>
< bean class =org.springframework.http.converter.json.GsonHttpMessageConverter>
< property name =gson>
< bean class =com.google.gson.Gsonfactory-bean =gsonBuilderfactory-method =create/>
< / property>
< / bean>
< / mvc:message-converters>
< / mvc:注释驱动>

< / beans>


How to configure Gson before constructing a GsonHttpMessageConverter?

I need to use @Expose and specify Date format.

解决方案

(New Method) Using Java Config

Extend WebMvcConfigurerAdapter or if you need more control use WebMvcConfigurationSupport.

@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;
    }

}

You can read more on how to customize provided configuration.

(Old Method) Using XML configuration

In DispatcherServlet context:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

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

这篇关于在使用GsonHttpMessageConverter之前,在Spring中配置Gson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆