最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年04月04日 11:33:47   作者:lory_li  
本篇文章主要介紹了SpringBoot使用自定義json解析器的使用方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

Spring-Boot是基于Spring框架的,它并不是對(duì)Spring框架的功能增強(qiáng),而是對(duì)Spring的一種快速構(gòu)建的方式。

Spring-boot應(yīng)用程序提供了默認(rèn)的json轉(zhuǎn)換器,為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> 

創(chuàng)建三個(gè)類: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 默認(rèn)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); 
 } 
} 

啟動(dòng)MainApp:訪問(wèn)http://localhost:8080/getUser,結(jié)果如下:

{"name":"張三","age":33,"createTime":"2018-04-04T03:03:08.534+0000"} 

可見:我們并未做任何配置,返回的卻是json數(shù)據(jù),可見Spring-Boot對(duì)json做了默認(rèn)實(shí)現(xiàn),使用的是內(nèi)置Jackson轉(zhuǎn)換器。

那么,下面看看如何使用自定義的json轉(zhuǎn)換器,這里以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.實(shí)現(xiàn)fastjson自定義json轉(zhuǎn)換的第一種方式,Spring-Boot實(shí)現(xiàn)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); 
  //創(chuàng)建fastjson轉(zhuǎn)換器實(shí)例 
  FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); 
  //配置對(duì)象 
  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); 
 } 
} 

啟動(dòng)程序:訪問(wèn)上面的路徑:瀏覽器會(huì)看到如下結(jié)果:

{ 
 "age":33, 
 "createTime":"2018-04-04 11:14", 
 "name":"張三" 
} 

2.使用@Bean注解注入fastjson轉(zhuǎn)換器:修改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); 
 } 
} 

訪問(wèn)結(jié)果是一樣的。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java基礎(chǔ)之java處理ip的工具類

    Java基礎(chǔ)之java處理ip的工具類

    這篇文章主要介紹了Java基礎(chǔ)應(yīng)用,使用java處理ip的工具類的相關(guān)資料,需要的朋友可以參考下
    2014-10-10
  • SpringBoot @ExceptionHandler與@ControllerAdvice異常處理詳解

    SpringBoot @ExceptionHandler與@ControllerAdvice異常處理詳解

    在Spring Boot應(yīng)用的開發(fā)中,不管是對(duì)底層數(shù)據(jù)庫(kù)操作,對(duì)業(yè)務(wù)層操作,還是對(duì)控制層操作,都會(huì)不可避免的遇到各種可預(yù)知的,不可預(yù)知的異常需要處理,如果每個(gè)處理過(guò)程都單獨(dú)處理異常,那么系統(tǒng)的代碼耦合度會(huì)很高,工作量大且不好統(tǒng)一,以后維護(hù)的工作量也很大
    2022-10-10
  • Java并發(fā)中的Fork/Join 框架機(jī)制詳解

    Java并發(fā)中的Fork/Join 框架機(jī)制詳解

    本文主要介紹了 Java 并發(fā)框架中的 Fork/Join 框架的基本原理和其使用的工作竊取算法(work-stealing)、設(shè)計(jì)方式和部分實(shí)現(xiàn)源碼,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • java判讀數(shù)組中是否有重復(fù)值的示例

    java判讀數(shù)組中是否有重復(fù)值的示例

    這篇文章主要介紹了java判讀數(shù)組中是否有重復(fù)值的示例,需要的朋友可以參考下
    2014-04-04
  • 詳解Spring Boot 項(xiàng)目中的 parent

    詳解Spring Boot 項(xiàng)目中的 parent

    這篇文章主要介紹了Spring Boot中parent作用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 詳解SpringCloudGateway內(nèi)存泄漏問(wèn)題

    詳解SpringCloudGateway內(nèi)存泄漏問(wèn)題

    這篇文章主要介紹了詳解SpringCloudGateway內(nèi)存泄漏問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 利用Jmeter發(fā)送Java請(qǐng)求的實(shí)戰(zhàn)記錄

    利用Jmeter發(fā)送Java請(qǐng)求的實(shí)戰(zhàn)記錄

    JMeter是Apache組織的開放源代碼項(xiàng)目,它是功能和性能測(cè)試的工具,100%的用java實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于如何利用Jmeter發(fā)送Java請(qǐng)求的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • MyBatis查詢結(jié)果resultType返回值類型的說(shuō)明

    MyBatis查詢結(jié)果resultType返回值類型的說(shuō)明

    這篇文章主要介紹了MyBatis查詢結(jié)果resultType返回值類型的說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • springboot中server.ssl.key-store配置路徑的問(wèn)題小結(jié)

    springboot中server.ssl.key-store配置路徑的問(wèn)題小結(jié)

    這篇文章主要介紹了springboot中server.ssl.key-store配置路徑的問(wèn)題,文中還記錄了Spring Boot SSL(https)實(shí)例,介紹在web程序中使用自簽名的SSL(HTTPS)證書及創(chuàng)建SSL認(rèn)證,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • Spring Boot調(diào)用 Shell 腳本實(shí)現(xiàn)看門狗功能

    Spring Boot調(diào)用 Shell 腳本實(shí)現(xiàn)看門狗功能

    這篇文章主要介紹了Spring Boot調(diào)用 Shell 腳本實(shí)現(xiàn)看門狗功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06

最新評(píng)論

惠水县| 闽清县| 凯里市| 射阳县| 江川县| 北海市| 都安| 上犹县| 喀什市| 贺州市| 伊吾县| 霍城县| 晋州市| 大邑县| 乌鲁木齐县| 金门县| 达拉特旗| 玉林市| 镇宁| 礼泉县| 五家渠市| 鄯善县| 扎鲁特旗| 江源县| 新闻| 临高县| 沂南县| 资阳市| 开江县| 郑州市| 绥宁县| 仪征市| 丁青县| 望都县| 响水县| 汶川县| 洛南县| 民和| 延长县| 保德县| 宿州市|