Spring Boot 使用第三方json解析 : fastjson

来源:互联网 发布:php use require 区别 编辑:程序博客网 时间:2024/05/21 17:08

Spring Boot 默认使用的是jackson解析json


导入pom依赖包:

<!-- 第三方json解析 : fastjson --><dependency>   <groupId>com.alibaba</groupId>   <artifactId>fastjson</artifactId>   <version>1.2.15</version></dependency>

需要在spring boot 的启动类中写一些配置:

方法一:继承以下 的类

extends WebMvcConfigurerAdapter

@SpringBootApplication@MapperScan("com.spring.boot.mapper") // 扫描该包下相应的class,主要是Mybatis的持久化类。public class SpringBootDemoApplication extends WebMvcConfigurerAdapter{   /**    * 方法一:    * @param args    */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {    super.configureMessageConverters(converters);    /**     * 先定义一个convert 转换消息的对象    * 添加fastJson 的配置信息,比如是否要格式化返回的json 数据    * 在 convert 中添加配置信息    * 将convert 添加到converts 中     */    // 先定义一个convert 转换消息的对象    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();    // 添加fastJson 的配置信息,比如是否要格式化返回的json 数据    FastJsonConfig fastJsonConfig = new FastJsonConfig();    fastJsonConfig.setSerializerFeatures(          SerializerFeature.PrettyFormat    );    // 在 convert 中添加配置信息   fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);    //将convert 添加到converts 中    converters.add(fastJsonHttpMessageConverter); }   public static void main(String[] args) {      SpringApplication.run(SpringBootDemoApplication.class, args);   }}


方法二:

@SpringBootApplication@MapperScan("com.spring.boot.mapper") // 扫描该包下相应的class,主要是Mybatis的持久化类。public class SpringBootDemoApplication{   /**    * 方法二    * @param args    */ @Bean public HttpMessageConverter fastJsonHttpMessageConverter(){   // 先定义一个convert 转换消息的对象   FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();   // 添加fastJson 的配置信息,比如是否要格式化返回的json 数据   FastJsonConfig fastJsonConfig = new FastJsonConfig();   fastJsonConfig.setSerializerFeatures(          SerializerFeature.PrettyFormat    );   // 在 convert 中添加配置信息   fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);    HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;    return converter; }   public static void main(String[] args) {      SpringApplication.run(SpringBootDemoApplication.class, args);   }}

测试:


在实体类中加入以下注解:

@JSONField(format = "yyyy-mm-dd HH:mm")

public class Girl {    private  String id;    private String name;    @JSONField(format = "yyyy-mm-dd HH:mm")    private Date creatTime;    public Girl() {    }    public Girl(String id, String name) {        this.id = id;        this.name = name;    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Date getCreatTime() {        return creatTime;    }    public void setCreatTime(Date creatTime) {        this.creatTime = creatTime;    }}

在controller 中进行测试:


@RestControllerpublic class HelloController {    /**     * Spring Boot 默认使用的json解析框架是 jackson     * @return     */    @RequestMapping("/getDemo")    public Girl getDemo(){        Girl girl = new Girl();        girl.setId("25325dfhdfh");        girl.setName("sgdsg");        girl.setCreatTime(new Date());        return girl;    }}

页面显示的时间格式是:yyyy-mm-dd HH:mm


阅读全文
0 0