當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
从零开始学springboot笔记(二)-Spring boot返回json数据(中文无乱码)
生活随笔
收集整理的這篇文章主要介紹了
从零开始学springboot笔记(二)-Spring boot返回json数据(中文无乱码)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
先創(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ù),具體如下:
說明: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("誰登錄看風(fēng)景"); demo.setAge(11); demo.setCreateTime(new Date()); return demo; } }直接訪問:
二:使用fastjson
需要添加依賴:
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version> </dependency>這里要說下很重要的話,官方文檔說的1.2.10以后,會有兩個方法支持HttpMessageconvert,一個是FastJsonHttpMessageConverter,支持4.2以下的版本,一個是FastJsonHttpMessageConverter4支持4.2以上的版本,具體有什么區(qū)別暫時沒有深入研究。這里也就是說:低版本的就不支持了,所以這里最低要求就是1.2.10+。
?配置fastjon(支持兩種方法)
第一種:
(1)啟動類繼承extends WebMvcConfigurerAdapter
(2)覆蓋方法configureMessageConverters
代碼如下:
@SpringBootApplication public class App extends WebMvcConfigurerAdapter {public static void main( String[] args ){System.out.println( "--------------開始啟動---------------!" ); SpringApplication.run(App.class, args); System.out.println( "--------------啟動成功---------------!" ); }@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);//處理中文亂碼問題List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastConverter);} }
重啟,然后訪問:
?第二種:在App.java啟動類中,注入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( "--------------開始啟動---------------!" ); SpringApplication.run(App.class, args); System.out.println( "--------------啟動成功---------------!" ); } @Beanpublic HttpMessageConverters fastJsonHttpMessageConverters() {FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //處理中文亂碼問題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)錯了,是:import org.springframework.http.MediaType;
訪問后的結(jié)果和第一種一樣;
第一種和第二種都可以使用,自己根據(jù)個人喜好選擇;
使用了fastjson框架后,還可以使用fastjson的注解對返回的json數(shù)據(jù)進(jìn)行特殊處理,如下:
@JSONField(format="yyyy-MM-dd HH:mm:ss")//格式化返回的日期 private Date createTime; @JSONField(serialize=false)//是否需要序列化屬性,如果為false,那么將不會返回該數(shù)據(jù)信息 private String remark;其他特性自己有興趣研究;
?
轉(zhuǎn)載于:https://www.cnblogs.com/YLQBL/p/10931311.html
總結(jié)
以上是生活随笔為你收集整理的从零开始学springboot笔记(二)-Spring boot返回json数据(中文无乱码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 土耳其电影公司选择Infortrend建
- 下一篇: swoole的process模块创建和使