本文将介绍三种方式实现json返回,分别是
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
这个依赖中默认加入了jackson-databind作为json处理器
public class Book {
private String name;
private String author;
@JsonIgnore
private Float price;
@JsonFormat(pattern = "yyyy-MM-dd")
private Date publicactionDate;
//TODO get和set方法
}
实体类中如果存在不想在json中展示的属性就用@JsonIgnore注解;如果想定义Date格式则用@JsonFormat(pattern=“yyyy-MM-dd”)
@Controller
public class BookController {
@GetMapping("/book")
@ResponseBody
public Book book() {
Book book = new Book();
book.setName("三体");
book.setAuthor("刘慈欣");
book.setPrice(50f);
book.setPublicactionDate(new Date());
return book;
}
}
如果频繁使用这个注释,则可以在类上使用组合注解@RestController
Gson是谷歌的一个开源json解析框架
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
public class Book {
private String name;
private String author;
protected Float price;
private Date publicactionDate;
//TODO get/set方法
}
@Controller
public class BookController {
@GetMapping("/book")
@ResponseBody
public Book book() {
Book book = new Book();
book.setName("三体");
book.setAuthor("刘慈欣");
book.setPrice(50f);
book.setPublicactionDate(new Date());
return book;
}
}
项目中没有提供GsonHttpMessageConverter实使用默认的GsonHttpMessageConverter
@Controller
public class GsonConfig {
@Bean
GsonHttpMessageConverter gsonHttpMessageConverter() {
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("yyyy-MM-dd");
builder.excludeFieldsWithModifiers(Modifier.PROTECTED);
Gson gson = builder.create();
converter.setGson(gson);
return converter;
}
}
其中设置了解析的日期格式为yyyy-MM-dd;当属性的修饰符为protected的时过滤掉
fastJson是阿里巴巴的一个开源json解析框架,是目前json解析最快的开源框架
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
fastjson集成完成之后不能马上使用,需要提供相应的HttpMessageConverter后才能使用
@Configuration
public class MyFastJsonConfig {
@Bean
FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd");
config.setCharset(Charset.forName("UTF-8"));
config.setSerializerFeatures(
SerializerFeature.WriteClassName,
SerializerFeature.WriteMapNullValue,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteNullStringAsEmpty
);
converter.setFastJsonConfig(config);
return converter;
}
}