spring boot 配置fastjson解析json

fastjson序列化属性

  • QuoteFieldNames 输出key时是否使用双引号,默认为true
  • WriteMapNullValue 是否输出值为null的字段,默认为false
  • WriteNullNumberAsZero 数值字段如果为null,输出为0,而非null
  • WriteNullListAsEmpty List字段如果为null,输出为[],而非null
  • WriteNullStringAsEmpty 字符类型字段如果为null,输出为”“,而非null
  • WriteNullBooleanAsFalse Boolean字段如果为null,输出为false,而非null

配置fastjson(2中方式)

一:继承WebMvcConfigurerAdapter

import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.http.converter.HttpMessageConverter;  
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;  
import com.alibaba.fastjson.serializer.SerializerFeature;  
import com.alibaba.fastjson.support.config.FastJsonConfig;  
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;  

@SpringBootApplication  
public class App extends WebMvcConfigurerAdapter{  
    public static void main(String[] args) {  
        SpringApplication.run(App.class, args);  
    }  

@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullListAsEmpty);

        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        converters.add(fastJsonHttpMessageConverter);
    }   
}   

二:注入bean

import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;  
import org.springframework.context.annotation.Bean;  
import org.springframework.http.converter.HttpMessageConverter;  
import com.alibaba.fastjson.serializer.SerializerFeature;  
import com.alibaba.fastjson.support.config.FastJsonConfig;  
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;  

@SpringBootApplication  
public class AppTwo{  

    public static void main(String[] args) {  
        SpringApplication.run(AppTwo.class, args);  
    }  

    @Bean  
    public HttpMessageConverters fastJsonHttpMessageConverters() {  
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();  
        FastJsonConfig fastJsonConfig = new FastJsonConfig();  
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);  
        fastConverter.setFastJsonConfig(fastJsonConfig);  
        HttpMessageConverter<?> converter = fastConverter;  
        return new HttpMessageConverters(converter);  
    }  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot中整合Fastjson需要进行以下步骤: 1.添加Fastjson依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.78</version> </dependency> ``` 2.配置FastjsonHttpMessageConverter 在Spring Boot中我们可以使用`@Configuration`和`@Bean`注解来配置`FastjsonHttpMessageConverter`,并将其添加到`HttpMessageConverters`中。 ```java @Configuration public class FastjsonConfig { @Bean public HttpMessageConverters fastjsonHttpMessageConverter() { //定义一个converters转换消息的对象 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //添加fastjson配置信息,比如:是否要格式化返回的json数据 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //在converter中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } } ``` 3.测试 在Controller中使用`@RestController`注解,并返回一个Object对象。Fastjson会自动将其转换为JSON格式。 ```java @RestController public class UserController { @GetMapping("/user") public Object getUser() { User user = new User(); user.setId(1L); user.setUsername("test"); user.setPassword("123456"); return user; } } ``` 访问http://localhost:8080/user,可以看到返回的JSON格式数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值