场景
1.SpringBoot默认配置的是Jackson。
实现
引入fastJson的依赖
<!-- fastjson的依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
实现方式1
通过继承WebMvcConfigurerAdapter来实现
找到springBoot的启动类
package com.example.demo;
import java.util.List;
import org.mybatis.spring.annotation.MapperScan;
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 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;
@MapperScan("com.example.demo.mapper")
@SpringBootApplication(scanBasePackages= {"com.example.demo.controller","com.example.demo.service"})
public class HelloSpringBootApplication extends WebMvcConfigurerAdapter{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//创建FastJson的消息转换器
FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
//创建FastJson的配置对象
FastJsonConfig config = new FastJsonConfig();
//对Json数据进行格式化
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
convert.setFastJsonConfig(config);
converters.add(convert);
}
public static void main(String[] args) {
SpringApplication.run(HelloSpringBootApplication.class, args);
}
}
通过extends WebMvcConfigurerAdapter并重写configureMessageConverters方法来实现。
新建Controller
package com.example.demo.controller;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.demo.pojo.User;
@Controller
public class TestFastjsonController {
@ResponseBody
@RequestMapping("/testFastJson")
public Object show() {
User user =new User();
user.setId(1);
user.setUsername("霸道");
user.setPassword("流氓气质");
user.setDate(new Date());
return user;
}
}
新建pojo
user.java
package com.example.demo.pojo;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
public class User {
private Integer id;
private String username;
private String password;
@JSONField(format="yyyy-MM-dd")
private Date date;
public Integer getId() {
return id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
运行看效果
实现方式2
在项目启动类中
package com.example.demo;
import java.util.List;
import org.mybatis.spring.annotation.MapperScan;
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 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;
@MapperScan("com.example.demo.mapper")
@SpringBootApplication(scanBasePackages= {"com.example.demo.controller","com.example.demo.service"})
public class HelloSpringBootApplication
{
@Bean
public HttpMessageConverters fastJsonMessageConverter() {
//创建FastJson的消息转换器
FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
//创建FastJson的配置对象
FastJsonConfig config = new FastJsonConfig();
//对Json数据进行格式化
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
convert.setFastJsonConfig(config);
HttpMessageConverter<?> con =convert;
return new HttpMessageConverters(con);
}
public static void main(String[] args) {
SpringApplication.run(HelloSpringBootApplication.class, args);
}
}
同样运行效果