springboot 默认的json解析框架是jackson
返回json 格式类
package com.imekaku; import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; /** * Created by lee on 2017/3/6. */ public class Demo { private int id; private String name; @JSONField(format = "yyyy-MM-dd HH:mm") private Date createTime; @JSONField(serialize = false) private String remarks; public String getRemarks() { return remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
控制类
package com.imekaku; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; /** * Created by lee on 2017/3/6. */ @RestController public class HelloController { @RequestMapping("/hello") public String hello() { return "spring-boot-hello"; } @RequestMapping("/getDemo") public Object getDemo() { Demo demo = new Demo(); demo.setId(1); demo.setName("张三"); demo.setCreateTime(new Date()); demo.setRemarks("这是备注信息"); return demo; } }
启动类
package com.imekaku; 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.web.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.http.converter.HttpMessageConverter; @SpringBootApplication // 指定springboot应用程序 public class SpringBootHelloApplication /*extends WebMvcConfigurerAdapter*/{ // @Override // public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // super.configureMessageConverters(converters); // // /* // 1. 需要先定义一个convert 转换消息的对象 // 2. 添加fastJson 配置信息,比如是否需要格式化返回json数据 // 3. 在convert中添加配置信息 // 4. 将convert添加到converters中 // */ // FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); // // FastJsonConfig fastJsonConfig = new FastJsonConfig(); // fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); // converters.add(fastJsonHttpMessageConverter); // } @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastJsonHttpMessageConverter; return new HttpMessageConverters(converter); } // 启动springboot 应用程序 public static void main(String[] args) { SpringApplication.run(SpringBootHelloApplication.class, args); } }
两种方法使用fastjson,1. 继承WebMvcConfigurerAdapter,覆盖configureMessageConverters,2. 添加一个bean
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.imekaku</groupId> <artifactId>spring-boot-hello</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-hello</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.27</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>