SpringBoot使用自定义json解析器的使用方法
程序员文章站
2023-12-03 09:39:10
spring-boot是基于spring框架的,它并不是对spring框架的功能增强,而是对spring的一种快速构建的方式。
spring-boot应用程序提供了默认的...
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 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); } }
访问结果是一样的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。