1. Project preparation
Directly use the source code of the previous chapter, Spring Boot Tutorial (10): Spring Boot integration jsp
2. Add dependencies
spring-boot-starter-web
Remove dependencies from it spring-boot-starter-json
and add fastjson dependencies:
<!-- Spring Boot web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
3. Configure fastjson
Method 1: Inject bean method
public HttpMessageConverters fastJsonHttpMessageConverters()
Add a method to the startup class Application :
package com.songguoliang.springboot;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import tk.mybatis.spring.annotation.MapperScan;
/**
* @Description
* @Author sgl
* @Date 2018-05-02 14:51
*/
@SpringBootApplication
@MapperScan("com.songguoliang.springboot.mapper")
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
/**
* 覆盖方法configureMessageConverters,使用fastJson
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
//1、定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3、在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、将convert添加到converters中
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
Method 2: The way to override configureMessageConverters()
the method
- Startup class implements the
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
interface - Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
method.
package com.songguoliang.springboot;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import tk.mybatis.spring.annotation.MapperScan;
import java.util.List;
/**
* @Description
* @Author sgl
* @Date 2018-05-02 14:51
*/
@SpringBootApplication
@MapperScan("com.songguoliang.springboot.mapper")
public class Application implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//1、定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3、在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、将convert添加到converters中
converters.add(fastConverter);
}
}
Fourth, create test objects
Create a model
package, and then create an FastjsonTest
entity in the package. In this entity, the value of the field is @JSONField
formatted through annotations, and the field is not returned to the foreground. The content is as follows:date
ignore
package com.songguoliang.springboot.model;
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;
/**
* @Description
* @Author sgl
* @Date 2018-05-08 13:50
*/
public class FastjsonTest {
private Integer id;
private String string;
/**
* 格式化日期
*/
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date date;
/**
* 转换为json时不包括该属性
*/
@JSONField(serialize = false)
private String ignore;
//省略getter、setter
}
5. Create Controller
Create one TestController
with the following content:
package com.songguoliang.springboot.controller;
import com.songguoliang.springboot.model.FastjsonTest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @Description
* @Author sgl
* @Date 2018-05-08 13:55
*/
@RestController
public class TestController {
@GetMapping("/fastjson")
public FastjsonTest getFastJson() {
FastjsonTest fastjsonTest = new FastjsonTest();
fastjsonTest.setId(1);
fastjsonTest.setString("fastjson test");
fastjsonTest.setIgnore("ignore field");
fastjsonTest.setDate(new Date());
return fastjsonTest;
}
}
6. Start the service to view the test results
Start the result, enter on the browser: http://localhost:8080/fastjson
, the result is as follows:
As you can see, date
the format of the field value is set by us yyyy-MM-dd HH:mm:ss
, and the ignore
field is not displayed.
Source code:
github
code cloud