时间:2021-05-02
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
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
【一】less简介LESS(是.less后缀名的文件)包含一套自定义的语法及一个解析器,我们根据这些语法定义自己的样式规则,这些规则最终会通过解析器,编译生成对
SpringBoot整合Gson整合Fastjson一、SpringBoot整合Gson1、pom依赖#在SpringBoot中给我们自带了json解析器,我们
前言:SpringBoot官网推荐使用HTML视图解析器,但是根据个人的具体业务也有可能使用到JSP视图解析器,所以这里我给大家简单介绍一下这两种视图解析器的具
本文实例讲述了帝国CMS自定义列表SQL调用方法。分享给大家供大家参考。具体方法如下:帝国CMS自定义列表可以用来实现特定的信息列表。自定义列表使用方法:用户进
这里是三岁,来和大家唠唠自定义函数,这一个神奇的东西,带大家白话玩转自定义函数自定义函数,编程里面的精髓!def自定义函数的必要函数:def使用方法:def函数