先创建json实体类,如下:
public class Demo {
private int age;
private String address;
private String name;
private String remark;
private Date createTime;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
一:使用jackson返回json数据,具体如下:
说明:spring boot默认的json解析框架是jsckson解析,所以不需要添加任何依赖;
代码如下:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
@RequestMapping("/getDemo")
public Demo getDemo(){
Demo demo = new Demo();
demo.setAddress("谁登录看风景");
demo.setAge(11);
demo.setCreateTime(new Date());
return demo;
}
}
直接访问:
二:使用fastjson
需要添加依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
这里要说下很重要的话,官方文档说的1.2.10以后,会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConverter,支持4.2以下的版本,一个是FastJsonHttpMessageConverter4支持4.2以上的版本,
具体有什么区别暂时没有深入研究。这里也就是说:低版本的就不支持了,所以这里最低要求就是1.2.10+。
配置fastjon(支持两种方法)
第一种:
(1)启动类继承extends WebMvcConfigurerAdapter
(2)覆盖方法configureMessageConverters
代码如下:
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter
{
public static void main( String[] args )
{
System.out.println( "--------------开始启动---------------!" );
SpringApplication.run(App.class, args);
System.out.println( "--------------启动成功---------------!" );
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// TODO Auto-generated method stub
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
}
重启,然后访问:
第二种:在App.java启动类中,注入Bean : HttpMessageConverters,代码如下:
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
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;
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter
{
public static void main( String[] args )
{
System.out.println( "--------------开始启动---------------!" );
SpringApplication.run(App.class, args);
System.out.println( "--------------启动成功---------------!" );
}
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
注意:包别导错了,是:import org.springframework.http.MediaType;
访问后的结果和第一种一样;
第一种和第二种都可以使用,自己根据个人喜好选择;
使用了fastjson框架后,还可以使用fastjson的注解对返回的json数据进行特殊处理,如下:
@JSONField(format="yyyy-MM-dd HH:mm:ss")//格式化返回的日期
private Date createTime;
@JSONField(serialize=false)//是否需要序列化属性,如果为false,那么将不会返回该数据信息
private String remark;
其他特性自己有兴趣研究;