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

所属分类: 软件编程 / java 阅读数: 16
收藏 0 赞 0 分享

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); 
 } 
} 

访问结果是一样的。

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

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多