Spring-Boot(一):使用自定義json解析器
阿新 • • 發佈:2019-01-06
Spring-Boot是基於Spring框架的,它並不是對Spring框架的功能增強,而是對Spring的一種快速構建的方式。
Spring-boot應用程式提供了預設的json轉換器,為Jackson。示例:
pom.xml中dependency配置:
<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>
建立三個類:MainApp.java和User.java以及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 預設json解析框架是Jackson * @return */ @RequestMapping("/getUser") public User getUser() { User u = new User(); u.setName("張三"); 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);
}
}
啟動MainApp:訪問http://localhost:8080/getUser,結果如下:
{"name":"張三","age":33,"createTime":"2018-04-04T03:03:08.534+0000"}
可見:我們並未做任何配置,返回的卻是json資料,可見Spring-Boot對json做了預設實現,使用的是內建Jackson轉換器。
那麼,下面看看如何使用自定義的json轉換器,這裡以fastjson為例:
首先,引入fastjson包,在pom中新增如下依賴:
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
為了方便看出效果:修改User類:
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.實現fastjson自定義json轉換的第一種方式,Spring-Boot實現WebMvcConventer介面:
修改MainApp如下:
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);
//建立fastjson轉換器例項
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//配置物件
FastJsonConfig config = new FastJsonConfig();
List<MediaType> mediaTypes = new ArrayList<>();
//中文編碼
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);
}
}
啟動程式:訪問上面的路徑:瀏覽器會看到如下結果:
{
"age":33,
"createTime":"2018-04-04 11:14",
"name":"張三"
}
2.使用@Bean註解注入fastjson轉換器:修改MainApp如下:
package com.springboot; 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.http.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.WebMvcConfigurer; import java.util.ArrayList; import java.util.List; @SpringBootApplication public class App{ //SpringBoot配置自定義json解析 @Bean public HttpMessageConverters fastJsonHttpMessageConventers(){ FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); config.setSerializerFeatures(SerializerFeature.PrettyFormat); converter.setSupportedMediaTypes(mediaTypes); converter.setFastJsonConfig(config); HttpMessageConverter<?> converter1 = converter; return new HttpMessageConverters(converter1); } /*@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { WebMvcConfigurer.super.configureMessageConverters(converters); //建立fastjson轉換器例項 FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); //配置物件 FastJsonConfig config = new FastJsonConfig(); List<MediaType> mediaTypes = new ArrayList<>(); //中文編碼 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(App.class,args); } }
訪問結果是一樣的。但是我發現這種bean注入的方式,當controller返回中文字串json格式時,支援不是太好,瀏覽器會出現亂碼。也不知道是什麼原因,因此還是使用的是第一種方式。