Springboot using Fastjson as the JSON parsing framework

Source: Internet
Author: User

Springboot using Fastjson as the JSON parsing framework

Springboot default comes with the JSON parsing framework, using Jackson by default, if you use Fastjson, you can configure the use

0, Build Springbboot Foundation Environment One, add dependence
<dependencies>    <dependency>        <groupId>com.alibaba</groupId>        <artifactId>fastjson</artifactId>        <version>1.2.15</version>    </dependency></dependencies>
Ii. configuration in the App.class (startup Class)

How to configure one (by inheritance)
1. Start class inheritance Webmvcconfigureradapter
2. Overriding the Configuremessageconverters method

    @Override    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {        super.configureMessageConverters(converters);        /*         * 1、需要先定义一个convert转换消息的对象         * 2、添加fastJson的配置信息,比如:是否要格式化返回json数据         * 3、在convert中添加配置信息         * 4、将convert添加到converters当中         *          */        //1、需要先定义一个·convert转换消息的对象;        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();        //2、添加fastjson的配置信息,比如 是否要格式化返回json数据        FastJsonConfig fastJsonConfig = new FastJsonConfig();        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);        //3、在convert中添加配置信息.        fastConverter.setFastJsonConfig(fastJsonConfig);        //4、将convert添加到converters当中.        converters.add(fastConverter);    }  

Configuration Mode II (via @bean injection)

@Beanpublic HttpMessageConverters fastJsonHttpMessageConverters() {    // 1、需要先定义一个 convert 转换消息的对象;    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();            //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;    FastJsonConfig fastJsonConfig = new FastJsonConfig();    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);           //3、在convert中添加配置信息.    fastConverter.setFastJsonConfig(fastJsonConfig);    HttpMessageConverter<?> converter = fastConverter;    return new HttpMessageConverters(converter);}
Second, the first configuration, test verification:

Prepare a controller.

import java.util.Date;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import com.xujie.pojo.Demo;@RestController // 包括了@ResponseBody和Controllerpublic class UserController {        @GetMapping("/test")    public Demo test() {        Demo demo = new Demo();        demo.setId(1);        demo.setCreateTime(new Date());        demo.setName("demo22");        demo.setRemarks("这里是备注信息,不应该返回");        return demo;    }}

Start class notation

Import Java.util.list;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; @SpringBootApplicationpublic class UserApplication extends Webmvcconfigureradapter {public static void main (string[] args) {Springapplication.run    (Userapplication.class, args);        } @Override public void Configuremessageconverters (list

Pojo class

import java.util.Date;import com.alibaba.fastjson.annotation.JSONField;public class Demo { private int id; private String name; //com.alibaba.fastjson.annotation.JSONField @JSONField(format="yyyy-MM-dd HH:mm") private Date createTime;//创建时间. /* * serialize:是否需要序列化属性. */ @JSONField(serialize=false) private String remarks;//备注信息. public String getRemarks() { return remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

Browser testing

* * Input Address: http://localhost:8080/test**

Return:
{ "createTime":"2018-02-23 12:06", "id":1, "name":"demo22" }

Springboot using Fastjson as the JSON parsing framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.