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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

从零开始学springboot笔记(二)-Spring boot返回json数据(中文无乱码)

發(fā)布時(shí)間:2024/4/14 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从零开始学springboot笔记(二)-Spring boot返回json数据(中文无乱码) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

先創(chuàng)建json實(shí)體類,如下:

public class Demo {private int age; private String address; private String name; private String remark; private Date createTime; public int getAge() {return age; }public void setAge(int age) {this.age = age; }public String getAddress() {return address; }public void setAddress(String address) {this.address = address; }public String getName() {return name; }public void setName(String name) {this.name = name; }public String getRemark() {return remark; }public void setRemark(String remark) {this.remark = remark; }public Date getCreateTime() {return createTime; }public void setCreateTime(Date createTime) {this.createTime = createTime; }}

一:使用jackson返回json數(shù)據(jù),具體如下:

說(shuō)明:spring boot默認(rèn)的json解析框架是jsckson解析,所以不需要添加任何依賴;

代碼如下:

@RestController public class HelloController {@RequestMapping("/hello")public String hello(){return "hello"; }@RequestMapping("/getDemo")public Demo getDemo(){Demo demo = new Demo(); demo.setAddress("誰(shuí)登錄看風(fēng)景"); demo.setAge(11); demo.setCreateTime(new Date()); return demo; } }

直接訪問(wèn):

二:使用fastjson

需要添加依賴:

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version> </dependency>這里要說(shuō)下很重要的話,官方文檔說(shuō)的1.2.10以后,會(huì)有兩個(gè)方法支持HttpMessageconvert,一個(gè)是FastJsonHttpMessageConverter,支持4.2以下的版本,一個(gè)是FastJsonHttpMessageConverter4支持4.2以上的版本,
具體有什么區(qū)別暫時(shí)沒(méi)有深入研究。這里也就是說(shuō):低版本的就不支持了,所以這里最低要求就是1.2.10+。

?配置fastjon(支持兩種方法)

第一種:

(1)啟動(dòng)類繼承extends WebMvcConfigurerAdapter
(2)覆蓋方法configureMessageConverters

代碼如下:

@SpringBootApplication public class App extends WebMvcConfigurerAdapter {public static void main( String[] args ){System.out.println( "--------------開(kāi)始啟動(dòng)---------------!" ); SpringApplication.run(App.class, args); System.out.println( "--------------啟動(dòng)成功---------------!" ); }@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {// TODO Auto-generated method stubsuper.configureMessageConverters(converters); FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//處理中文亂碼問(wèn)題List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastConverter);} }

重啟,然后訪問(wèn):

?第二種:在App.java啟動(dòng)類中,注入Bean : HttpMessageConverters,代碼如下:

import java.util.ArrayList; import java.util.List; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.http.MediaType; 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; @SpringBootApplication public class App extends WebMvcConfigurerAdapter {public static void main( String[] args ){System.out.println( "--------------開(kāi)始啟動(dòng)---------------!" ); SpringApplication.run(App.class, args); System.out.println( "--------------啟動(dòng)成功---------------!" ); } @Beanpublic HttpMessageConverters fastJsonHttpMessageConverters() {FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //處理中文亂碼問(wèn)題List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); }}

注意:包別導(dǎo)錯(cuò)了,是:import org.springframework.http.MediaType;

訪問(wèn)后的結(jié)果和第一種一樣;

第一種和第二種都可以使用,自己根據(jù)個(gè)人喜好選擇;

使用了fastjson框架后,還可以使用fastjson的注解對(duì)返回的json數(shù)據(jù)進(jìn)行特殊處理,如下:

@JSONField(format="yyyy-MM-dd HH:mm:ss")//格式化返回的日期 private Date createTime; @JSONField(serialize=false)//是否需要序列化屬性,如果為false,那么將不會(huì)返回該數(shù)據(jù)信息 private String remark;

其他特性自己有興趣研究;

?

轉(zhuǎn)載于:https://www.cnblogs.com/YLQBL/p/10931311.html

總結(jié)

以上是生活随笔為你收集整理的从零开始学springboot笔记(二)-Spring boot返回json数据(中文无乱码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。