查看: 446|回复: 0

[Java语言] SpringBoot使用自定义json解析器的使用方法

发表于 2018-5-2 08:00:01

Spring-Boot是基于Spring框架的,它并不是对Spring框架的功能增强,而是对Spring的一种快速构建的方式。

Spring-boot应用程序提供了默认的json转换器,为Jackson。示例:

pom.xml中dependency配置:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.qinker</groupId>
  5. <artifactId>spring-boot</artifactId>
  6. <packaging>war</packaging>
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>2.0.0.RELEASE</version>
  11. </parent>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>spring-boot</name>
  14. <url>http://maven.apache.org</url>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <java.version>9</java.version>
  18. </properties>
  19. <dependencies>
  20. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. </dependencies>
  26. <build>
  27. <finalName>spring-boot</finalName>
  28. </build>
  29. </project>
复制代码

创建三个类:MainApp.java和User.java以及HelloController.java:

  1. package com.springboot;
  2. import java.util.Date;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. public class HelloController {
  7. @RequestMapping("/hello")
  8. public String hello() {
  9. return "hello,SpringBoot";
  10. }
  11. /**
  12. * Spring boot 默认json解析框架是Jackson
  13. * @return
  14. */
  15. @RequestMapping("/getUser")
  16. public User getUser() {
  17. User u = new User();
  18. u.setName("张三");
  19. u.setAge(33);
  20. u.setCreateTime(new Date());
  21. return u;
  22. }
  23. }
复制代码
  1. package com.springboot;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. public class User implements Serializable{
  5. private String name;
  6. private int age;
  7. private Date createTime;
  8. public String getName() {
  9. return name;
  10. }
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. public int getAge() {
  15. return age;
  16. }
  17. public void setAge(int age) {
  18. this.age = age;
  19. }
  20. public Date getCreateTime() {
  21. return createTime;
  22. }
  23. public void setCreateTime(Date createTime) {
  24. this.createTime = createTime;
  25. }
  26. }
复制代码
  1. package com.springboot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class MainApp{
  6. public static void main(String[] args) {
  7. SpringApplication.run(MainApp.class, args);
  8. }
  9. }
复制代码

启动MainApp:访问http://localhost:8080/getUser,结果如下:

  1. {"name":"张三","age":33,"createTime":"2018-04-04T03:03:08.534+0000"}
复制代码

可见:我们并未做任何配置,返回的却是json数据,可见Spring-Boot对json做了默认实现,使用的是内置Jackson转换器。

那么,下面看看如何使用自定义的json转换器,这里以fastjson为例:

首先,引入fastjson包,在pom中添加如下依赖:

  1. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>1.2.47</version>
  6. </dependency>
复制代码

为了方便看出效果:修改User类:

  1. package com.springboot;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. import com.alibaba.fastjson.annotation.JSONField;
  5. @SuppressWarnings("serial")
  6. public class User implements Serializable{
  7. private String name;
  8. private int age;
  9. @JSONField(format="yyyy-MM-dd HH:mm")
  10. private Date createTime;
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. public Date getCreateTime() {
  24. return createTime;
  25. }
  26. public void setCreateTime(Date createTime) {
  27. this.createTime = createTime;
  28. }
  29. }
复制代码

1.实现fastjson自定义json转换的第一种方式,Spring-Boot实现WebMvcConventer接口:

修改MainApp如下:

  1. package com.springboot;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.http.MediaType;
  7. import org.springframework.http.converter.HttpMessageConverter;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  9. import com.alibaba.fastjson.serializer.SerializerFeature;
  10. import com.alibaba.fastjson.support.config.FastJsonConfig;
  11. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  12. @SpringBootApplication
  13. public class MainApp implements WebMvcConfigurer{
  14. @Override
  15. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  16. WebMvcConfigurer.super.configureMessageConverters(converters);
  17. //创建fastjson转换器实例
  18. FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
  19. //配置对象
  20. FastJsonConfig config = new FastJsonConfig();
  21. List<MediaType> mediaTypes = new ArrayList<>();
  22. //中文编码
  23. MediaType mediaType = MediaType.APPLICATION_JSON_UTF8;
  24. mediaTypes.add(mediaType);
  25. config.setSerializerFeatures(SerializerFeature.PrettyFormat);
  26. converter.setSupportedMediaTypes(mediaTypes);
  27. converter.setFastJsonConfig(config);
  28. converters.add(converter);
  29. }
  30. public static void main(String[] args) {
  31. SpringApplication.run(MainApp.class, args);
  32. }
  33. }
复制代码

启动程序:访问上面的路径:浏览器会看到如下结果:

  1. {
  2. "age":33,
  3. "createTime":"2018-04-04 11:14",
  4. "name":"张三"
  5. }
复制代码

2.使用@Bean注解注入fastjson转换器:修改MainApp如下:

  1. package com.springboot;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.http.MediaType;
  9. import com.alibaba.fastjson.serializer.SerializerFeature;
  10. import com.alibaba.fastjson.support.config.FastJsonConfig;
  11. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  12. @SpringBootApplication
  13. public class MainApp{
  14. [url=home.php?mod=space&uid=4377]@Bean[/url]
  15. public HttpMessageConverters fastJsonHttpMessageConventers() {
  16. FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
  17. FastJsonConfig config = new FastJsonConfig();
  18. config.setSerializerFeatures(SerializerFeature.PrettyFormat);
  19. List<MediaType> mediaTypes = new ArrayList<>();
  20. mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
  21. converter.setSupportedMediaTypes(mediaTypes);
  22. return new HttpMessageConverters(converter);
  23. }
  24. public static void main(String[] args) {
  25. SpringApplication.run(MainApp.class, args);
  26. }
  27. }
复制代码

访问结果是一样的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报