springboot整合web开发

springboot整合web开发

web依赖自带返回json的格式

这个依赖中默认加入了 jackson-databind

作为 JSON 器,此时不需要添 额外的 JSON

理器就能返回一段 JSON 了。

自定义转换器:

1.集成Gson

常见的json处理器除了jackson-databind之外,还有Gson和fastjson,

Gson是google的开源json解析框架,使用Gson,首先需要去除默认的jackson-databind,然后加入Gson依赖

pom文件配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
</dependency>

由于 Spring Boot 中默 Gson 自动转换类 GsonHttpMessageConvertersConfiguration,

因此Gson 的依赖添加成功后 可以像使用 jackson-databind 那样直接使用 Gson 。但是在 Gson 进行

转换时 如果想对日期数据进行格式化, 那么还需要开发者自定义 HttpMessageConverter 。

@Configuration
public class GsonConfig {
    //自己提供一个gsonHtppMessageConverter的实例
    @Bean
    GsonHttpMessageConverter gsonHttpMessageConverter(){
        GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
            GsonBuilder builder=new GsonBuilder();
            //设置日期格式
            builder.setDateFormat("yyyy-MM-dd");
            //过滤protected修饰的字段。
            builder.excludeFieldsWithModifiers(Modifier.PROTECTED);
            //创建Gson对象将json对象交给GsonHttpMessageConevter实例并返回
            Gson gson=builder.create();
            converter.setGson(gson);
            return converter;
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qN1mKnPE-1618484134202)(C:%5CUsers%5Cwuyuhong%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20210414105311179.png)]

2.集成fastjson

​ fastjson是阿里巴巴的一个开源JSON解析框架,是目前JSON解析速度最快的开源框架。fastjson继承完之后不能马上使用,需要提供相应的HttpMessageConverter后才能使用

pom文件加入

    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.8.2</version>
</dependency>

fastjson的HttpMessageConverter

/**自定义 MyFastJsonConfig ,完成对 FastJsonHttpMessageConverter Bean 的提供
 * @author wuyuhong
 * @date 2021年04月14日 10:56
 */
@Configuration
public class MyFastJsonConfig {
    @Bean
    FastJsonHttpMessageConverter fastJsonHttpMessageConever(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        // 配置了JSON 解析过程的一些细节,例如日期格式、数据编码、是否在生成的
        //JSON 中输出类名 、是否输出 value 为null 的数据、生成的 JSON 格式化、空集合输出[]而非
        //null 、空字符串输出""而非 null 等基本配直
            config.setDateFormat("yyyy-MM-dd");
            config.setCharset(Charset.forName("UTF-8"));
            config.setSerializerFeatures(
                    SerializerFeature.WriteClassName,
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.PrettyFormat,
                    SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.WriteNullStringAsEmpty
            );
            converter.setFastJsonConfig(config);
            return converter;
    }
}

config配置后还需要设置返回编码,application.properties中配置,不然中文乱码

server.servlet.encoding.force-response=true

另外一种方式实现webMvcConfigurer类

//@Configuration
public class GsonConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        // 配置了JSON 解析过程的一些细节,例如日期格式、数据编码、是否在生成的
        //JSON 中输出类名 、是否输出 value 为null 的数据、生成的 JSON 格式化、空集合输出[]而非
        //null 、空字符串输出""而非 null 等基本配直
        config.setDateFormat("yyyy-MM-dd");
        config.setCharset(Charset.forName("UTF-8"));
        config.setSerializerFeatures(
                SerializerFeature.WriteClassName,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty
        );
        converter.setFastJsonConfig(config);
        converters.add(converter);

    }
  
}

.自定义 MyWebMvcConfig 类并实现 WebMvcConfigurer 接口中的 configureMessageConverters方法

.将自定义的fastJsonHttpMessageConverter 加入 converters中
$$
如果使用了 Gson ,也可以采用这种方式配置,但是不推荐。 因为当项目中没有

GsonH Messag Conv巳出 Spring Boot 自己会提供一个 GsonHttpMessageConverter ,此

时重写 configureMessageConverters 方法 参数 converters 中已经有 GsonHttpMessageConverter的实例了,需要替换已有的 Gso nHttpMessageConv er 实例,操作比较麻烦,所以对于 Gson,推荐直接提供 GsonHttpMessageConverter
c o n f i g u r e M e s s a g e C o n v e r t e r s 方 法 参 数 c o n v e r t e r s 中 已 经 有 G s o n H t t p M e s s a g e C o n v e r t e r 的 实 例 了 , 需 要 替 换 已 有 的 G s o n H t t p M e s s a g e C o n v e r 实 例 , 操 作 比 较 麻 烦 , 所 以 对 于 G s o n , 推 荐 直 接 提 供 G s o n H t t p M e s s a g e C o n v e r t e r configureMessageConverters 方法 参数 converters 中已经有 GsonHttpMessageConverter的实例了,需要替换已有的 Gso nHttpMessageConv er 实例,操作比较麻烦,所以对于 Gson,推荐直接提供 GsonHttpMessageConverter configureMessageConvertersconvertersGsonHttpMessageConverterGsonHttpMessageConverGson,GsonHttpMessageConverter

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值