服务器之家

服务器之家 > 正文

SpringBoot使用自定义json解析器的使用方法

时间:2021-04-20 14:00     来源/作者:lory_li

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

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

pom.xml中dependency配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
 }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
 
}
?
1
2
3
4
5
6
7
8
9
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,结果如下:

?
1
{"name":"张三","age":33,"createtime":"2018-04-04t03:03:08.534+0000"}

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

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

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

?
1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  <dependency>
   <groupid>com.alibaba</groupid>
   <artifactid>fastjson</artifactid>
   <version>1.2.47</version>
  </dependency>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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如下: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
 }
}

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

?
1
2
3
4
5
{
 "age":33,
 "createtime":"2018-04-04 11:14",
 "name":"张三"
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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);
 }
}

访问结果是一样的。

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

原文链接:https://blog.csdn.net/lory_li/article/details/79814449

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部