- 引入包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
- spring boot 默认使用的是json的jackson,这里替换成了fastjson
第一种:
package com.jf.mavenLogin;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
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;/**
* Hello world!
*
*/@ComponentScan(basePackages = "com.jf.controller")
@EnableAutoConfiguration
public class App extends WebMvcConfigurerAdapter
{
/**
* spring boot 默认使用的是json的jackson,这里替换成了fastjson
* 也可以采用注入bean的方式
* @Description
* @Param
* @Return
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//定义convert的转换对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
//把配置信息添加到对象中
fastConverter.setFastJsonConfig(fastJsonConfig);
//把新建的对象替换返回
converters.add(fastConverter);
}
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
第二种:
package com.jf.mavenLogin;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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;/**
* Hello world!
*
*/@ComponentScan(basePackages = "com.jf.controller")
@EnableAutoConfiguration
public class AppBeanJson
{
/**
* spring boot 默认使用的是json的jackson,这里替换成了fastjson
* 也可以采用注入bean的方式
* @Description
* @Param
* @Return
*/
// @Override
// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// super.configureMessageConverters(converters);
//
// //定义convert的转换对象
// FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// //添加fastjson的配置信息
// FastJsonConfig fastJsonConfig = new FastJsonConfig();
// fastJsonConfig.setSerializerFeatures(
// SerializerFeature.PrettyFormat
// );
//
// //把配置信息添加到对象中
// fastConverter.setFastJsonConfig(fastJsonConfig);
//
// //把新建的对象替换返回
// converters.add(fastConverter);
//
// }
@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);
}
public static void main( String[] args )
{
SpringApplication.run(AppBeanJson.class, args);
}
}
对象
package com.jf.entity;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
/**
* @Description
* @Author zengzhiqiang
* @Date 2019年1月28日
*/public class Student {
@JSONField(format="yyyy-MM-dd HH:mm")
private Date timed;
private String id ;public Date getTimed() {
return timed;
}public void setTimed(Date timed) {
this.timed = timed;
}public String getId() {
return id;
}public void setId(String id) {
this.id = id;
}
}
package com.jf.controller;
import java.util.Date;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jf.entity.Student;/**
* @Description
* @Author
* @Date 2018年7月30日
*/
@RestController
public class HelloSpringBootController {
@RequestMapping("/hello")
public Student hello(){
Student s = new Student();
s.setId("111");
s.setTimed(new Date());
s.setDes("2222222222555");
return s;
}
}