Spring-Boot is based on the Spring framework. It is not an enhancement to the Spring framework's functionality, but a way to build Spring quickly.
Spring-boot application provides the default json converter for Jackson. Example:
Dependency configuration in pom.xml:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.qinker</groupId> <artifactId>spring-boot</artifactId> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <version>0.0.1-SNAPSHOT</version> <name>spring-boot</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>9</java.version> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <finalName>spring-boot</finalName> </build> </project>
Create three classes: MainApp.java and User.java and HelloController.java:
package com.springboot; import java.util.Date; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello() { return "hello,SpringBoot"; } /** * Spring boot The default json resolution framework is Jackson * @return */ @RequestMapping("/getUser") public User getUser() { User u = new User(); u.setName("Zhang San"); u.setAge(33); u.setCreateTime(new Date()); return u; } } package com.springboot; import java.io.Serializable; import java.util.Date; public class User implements Serializable{ private String name; private int age; private Date createTime; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } } package com.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MainApp{ public static void main(String[] args) { SpringApplication.run(MainApp.class, args); } }Start MainApp: Visit http://localhost:8080/getUser, the result is as follows:
{"name":"Zhang San","age":33,"createTime":"2018-04-04T03:03:08.534+0000"}It can be seen that we did not make any configuration, but we returned json data. It can be seen that Spring-Boot has made a default implementation of json and uses the built-in Jackson converter.
So, let’s take fastjson as an example:
First, introduce the fastjson package and add the following dependencies in the pom:
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
To facilitate the effect, modify the User class:
package com.springboot; import java.io.Serializable; import java.util.Date; import com.alibaba.fastjson.annotation.JSONField; @SuppressWarnings("serial") public class User implements Serializable{ private String name; private int age; @JSONField(format="yyyy-MM-dd HH:mm") private Date createTime; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }1. The first way to implement fastjson custom json conversion, Spring-Boot implements the WebMvcConventer interface:
Modify MainApp as follows:
package com.springboot; import java.util.ArrayList; import java.util.List; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication public class MainApp implements WebMvcConfigurer{ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { WebMvcConfigurer.super.configureMessageConverters(converters); //Create a fastjson converter instance FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); //Configuration object FastJsonConfig config = new FastJsonConfig(); List<MediaType> mediaTypes = new ArrayList<>(); //Chinese encoding MediaType mediaType = MediaType.APPLICATION_JSON_UTF8; mediaTypes.add(mediaType); config.setSerializerFeatures(SerializerFeature.PrettyFormat); converter.setSupportedMediaTypes(mediaTypes); converter.setFastJsonConfig(config); converters.add(converter); } public static void main(String[] args) { SpringApplication.run(MainApp.class, args); } }Start the program: Access the above path: The browser will see the following results:
{ "age":33, "createTime":"2018-04-04 11:14", "name":"Zhang San" } 2. Inject fastjson converter using @Bean annotation: modify MainApp as follows:
package com.springboot; import java.util.ArrayList; import java.util.List; 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.MediaType; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication public class MainApp{ @Bean public HttpMessageConverters fastJsonHttpMessageConventers() { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures(SerializerFeature.PrettyFormat); List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); converter.setSupportedMediaTypes(mediaTypes); return new HttpMessageConverters(converter); } public static void main(String[] args) { SpringApplication.run(MainApp.class, args); } }The access result is the same.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.