SpringBoot使用自定义json解析器的使用方法
Spring-Boot是基于Spring框架的,它并不是对Spring框架的功能增强,而是对Spring的一种快速构建的方式。
Spring-boot应用程序提供了默认的json转换器,为Jackson。示例:
pom.xml中dependency配置:
4.0.0 com.qinker spring-boot war org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE 0.0.1-SNAPSHOT spring-boot http://maven.apache.org UTF-8 9 org.springframework.boot spring-boot-starter-web spring-boot
创建三个类:MainApp.java和User.java以及HelloController.java:
packagecom.springboot; importjava.util.Date; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; @RestController publicclassHelloController{ @RequestMapping("/hello") publicStringhello(){ return"hello,SpringBoot"; } /** *Springboot默认json解析框架是Jackson *@return */ @RequestMapping("/getUser") publicUsergetUser(){ Useru=newUser(); u.setName("张三"); u.setAge(33); u.setCreateTime(newDate()); returnu; } }
packagecom.springboot; importjava.io.Serializable; importjava.util.Date; publicclassUserimplementsSerializable{ privateStringname; privateintage; privateDatecreateTime; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicDategetCreateTime(){ returncreateTime; } publicvoidsetCreateTime(DatecreateTime){ this.createTime=createTime; } }
packagecom.springboot; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication publicclassMainApp{ publicstaticvoidmain(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中添加如下依赖:
com.alibaba fastjson 1.2.47
为了方便看出效果:修改User类:
packagecom.springboot; importjava.io.Serializable; importjava.util.Date; importcom.alibaba.fastjson.annotation.JSONField; @SuppressWarnings("serial") publicclassUserimplementsSerializable{ privateStringname; privateintage; @JSONField(format="yyyy-MM-ddHH:mm") privateDatecreateTime; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicDategetCreateTime(){ returncreateTime; } publicvoidsetCreateTime(DatecreateTime){ this.createTime=createTime; } }
1.实现fastjson自定义json转换的第一种方式,Spring-Boot实现WebMvcConventer接口:
修改MainApp如下:
packagecom.springboot; importjava.util.ArrayList; importjava.util.List; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.http.MediaType; importorg.springframework.http.converter.HttpMessageConverter; importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer; importcom.alibaba.fastjson.serializer.SerializerFeature; importcom.alibaba.fastjson.support.config.FastJsonConfig; importcom.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication publicclassMainAppimplementsWebMvcConfigurer{ @Override publicvoidconfigureMessageConverters(List>converters){ WebMvcConfigurer.super.configureMessageConverters(converters); //创建fastjson转换器实例 FastJsonHttpMessageConverterconverter=newFastJsonHttpMessageConverter(); //配置对象 FastJsonConfigconfig=newFastJsonConfig(); List mediaTypes=newArrayList<>(); //中文编码 MediaTypemediaType=MediaType.APPLICATION_JSON_UTF8; mediaTypes.add(mediaType); config.setSerializerFeatures(SerializerFeature.PrettyFormat); converter.setSupportedMediaTypes(mediaTypes); converter.setFastJsonConfig(config); converters.add(converter); } publicstaticvoidmain(String[]args){ SpringApplication.run(MainApp.class,args); } }
启动程序:访问上面的路径:浏览器会看到如下结果:
{ "age":33, "createTime":"2018-04-0411:14", "name":"张三" }
2.使用@Bean注解注入fastjson转换器:修改MainApp如下:
packagecom.springboot; importjava.util.ArrayList; importjava.util.List; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.boot.autoconfigure.http.HttpMessageConverters; importorg.springframework.context.annotation.Bean; importorg.springframework.http.MediaType; importcom.alibaba.fastjson.serializer.SerializerFeature; importcom.alibaba.fastjson.support.config.FastJsonConfig; importcom.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication publicclassMainApp{ @Bean publicHttpMessageConvertersfastJsonHttpMessageConventers(){ FastJsonHttpMessageConverterconverter=newFastJsonHttpMessageConverter(); FastJsonConfigconfig=newFastJsonConfig(); config.setSerializerFeatures(SerializerFeature.PrettyFormat); ListmediaTypes=newArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); converter.setSupportedMediaTypes(mediaTypes); returnnewHttpMessageConverters(converter); } publicstaticvoidmain(String[]args){ SpringApplication.run(MainApp.class,args); } }
访问结果是一样的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。