日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

FastJson/spring boot: json输出

發布時間:2023/12/13 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 FastJson/spring boot: json输出 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.引入FastJson依賴包

<!-- FastJson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency>

  

pom.xml參考

<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.muyang</groupId><artifactId>boot1</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>boot1</name><url>http://maven.apache.org</url><!-- Inherit defaults from Spring Boot --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version><!--<version>2.0.1.RELEASE</version>--></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><!-- 自動依賴parent里面的版本<version></version>--></dependency><!-- dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency--><!-- FastJson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><build><finalName>boot1</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${java.version}</source><target>${java.version}</target> </configuration></plugin></plugins></build></project>

  

2.App.java繼承WebMvcConfigurerAdapter,然后復寫configureMessageConverters方法,加入自定義的FastJson配置

/*** 復寫configureMessageConverters*/@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {// TODO Auto-generated method stub//super.configureMessageConverters(converters);/** 1、需要先定義一個 convert 轉換消息的對象;* 2、添加fastJson 的配置信息,比如:是否要格式化返回的json數據;* 3、在convert中添加配置信息.* 4、將convert添加到converters當中.* */// 1、需要先定義一個 convert 轉換消息的對象;FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();//2、添加fastJson 的配置信息,比如:是否要格式化返回的json數據;FastJsonConfig fastJsonConfig = new FastJsonConfig();fastJsonConfig.setSerializerFeatures( SerializerFeature.PrettyFormat);//3、在convert中添加配置信息.fastConverter.setFastJsonConfig(fastJsonConfig);//4、將convert添加到converters當中.converters.add(fastConverter);}

  

APP.JAVA參考

package com.muyang.boot1;import java.util.List;import org.apache.log4j.Logger; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;/*** Hello world!**/ @SpringBootApplication public class App extends WebMvcConfigurerAdapter {private static Logger logger = Logger.getLogger(App.class);/*** 復寫configureMessageConverters*/@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {// TODO Auto-generated method stub//super.configureMessageConverters(converters);/** 1、需要先定義一個 convert 轉換消息的對象;* 2、添加fastJson 的配置信息,比如:是否要格式化返回的json數據;* 3、在convert中添加配置信息.* 4、將convert添加到converters當中.* */// 1、需要先定義一個 convert 轉換消息的對象;FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();//2、添加fastJson 的配置信息,比如:是否要格式化返回的json數據;FastJsonConfig fastJsonConfig = new FastJsonConfig();fastJsonConfig.setSerializerFeatures( SerializerFeature.PrettyFormat);//3、在convert中添加配置信息.fastConverter.setFastJsonConfig(fastJsonConfig);//4、將convert添加到converters當中.converters.add(fastConverter);}public static void main( String[] args ){//System.out.println( "Hello World!" );\SpringApplication.run(App.class, args);logger.info("--sprint-boot-");}}

  

3.建立Demo.java類,用來創建json輸出之前的對象,并創建new Date()日期格式化屬性

JSONField(format="yyyy-MM-dd HH:mm")

private Date createTime

/*** 格式化時間*/@JSONField(format="yyyy-MM-dd HH:mm")private Date createTime; /*** 不顯示json*/@JSONField(serialize=false)private String remarks;

  

Demo.java參考

package com.muyang.boot1;import java.util.Date;import com.alibaba.fastjson.annotation.JSONField;public class Demo {private int id;private String name;/*** 格式化時間*/@JSONField(format="yyyy-MM-dd HH:mm")private Date createTime; /*** 不顯示json*/@JSONField(serialize=false)private String remarks;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public String getRemarks() {return remarks;}public void setRemarks(String remarks) {this.remarks = remarks;}}

  

?

4.HelloController.java控制器

普通輸出

@RequestMapping(value="/hello")public String hello(){return "hello";}

  

?

json輸出

/** * charset解決中文亂碼 **/ @RequestMapping(value="/getDemo", produces = "application/json; charset=utf-8")public Demo getDemo(){Demo demo = new Demo();demo.setId(1);demo.setName("張三");demo.setCreateTime(new Date());demo.setRemarks("這是備注信息");return demo;}

  

?

HelloController.java參考

package com.muyang.boot1;import java.util.Date;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;@RestController public class HelloController {@RequestMapping(value="/hello")public String hello(){return "hello";}@RequestMapping(value="/getDemo", produces = "application/json; charset=utf-8")public Demo getDemo(){Demo demo = new Demo();demo.setId(1);demo.setName("張三");demo.setCreateTime(new Date());demo.setRemarks("這是備注信息");return demo;}}

  

?

轉載于:https://www.cnblogs.com/achengmu/p/9295088.html

總結

以上是生活随笔為你收集整理的FastJson/spring boot: json输出的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。