通過spring-boot 返回json數(shù)據(jù)
1.創(chuàng)建一個實體類
2.以json的格式將實體類返回去
3.使用第三方的json框架
4.spring-boot的熱部署
1.創(chuàng)建一個實體類
在上一章中創(chuàng)建一個spring-boot工程:
new -> 一個新class懊直,取名為JSONDemo剩蟀,代碼如下:
package com.avalanching.spring_hellow.model;
import java.util.Date;
public class JSONDemo {
private Integer id;
private String name;
private Date createTime;
private String remark;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
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;
}
}
2.以json的格式將實體類返回去
在上一章創(chuàng)建的controller中輸入添加一個方法
@RequestMapping("/getJson")
public JSONDemo getJson() {
JSONDemo demo = new JSONDemo();
demo.setId(1);
demo.setName("avalanching");
demo.setRemark("hello world");
demo.setCreateTime(new Date());
return demo;
}
啟動工程蕊肥,等啟動完畢打開瀏覽器輸入地址:http://localhost:8080/getJson
3.使用第三方的json框架
3.1多層級的json結構
spring-boot自帶了jackson自動java對象轉化為json返回,這一點十分方便疆偿,還能多個層級結構
接觸到的請求返回格式
{"code":0,"message":"xxxxx","data":.....}
簡單實現(xiàn)一下格式
新建一個BaseJSONEntity,代碼如下:
package com.avalanching.spring_hellow.model;
public class BaseJSONEntity {
private Integer code;
private String message;
private Object data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
在controller中添加方法:
@RequestMapping("/jsondemo")
public BaseJSONEntity getDemo() {
BaseJSONEntity entity = new BaseJSONEntity();
entity.setCode(0);
entity.setMessage("success");
JSONDemo demo = new JSONDemo();
demo.setId(1);
demo.setName("avalanching");
demo.setRemark("hello world");
demo.setCreateTime(new Date());
entity.setData(demo);
return entity;
}
啟動工程讼载,等啟動完畢打開瀏覽器輸入地址:http://localhost:8080/jsondemo
3.2使用第三方的json處理方案
這里以fastjson為例子
3.2.1 在導入fastjson的jar
在pom.xml中<dependencies></dependencies>之間配置fastjson的信息陶冷,請在:https://mvnrepository.com 去搜索相關的jar信息
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
保存pom.xml
3.2.2 在工程入口配置信息
1.方式一,通過繼承WebMvcConfigurerAdapter桥嗤,重寫public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
的方式去添加
app.js中的代碼如下:
package com.avalanching.spring_hellow;
import java.util.List;
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用標示這是一個Spring boot的啟動類
*/
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter
{
public static void main( String[] args ) {
SpringApplication.run(App.class, args);
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// TODO Auto-generated method stub
super.configureMessageConverters(converters);
// 1.需要意義一個 FastJsonHttpMessageConverter须妻,比如是否格式化返回json
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加 fastJson的配置信息, 比如是否要格式化返回 json 數(shù)據(jù)
FastJsonConfig jsonConfig = new FastJsonConfig();
jsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(jsonConfig);
// 4.將converter添加到converters中
converters.add(fastConverter);
}
}
方式二泛领,通過@Bean的注解添加配置
app.js中的代碼如下:
package com.avalanching.spring_hellow;
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.converter.HttpMessageConverter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
/**
* Hello world!
* SpringBootApplication用標示這是一個Spring boot的啟動類
*/
@SpringBootApplication
public class App
{
public static void main( String[] args ) {
SpringApplication.run(App.class, args);
}
@Bean
public HttpMessageConverters fastJsonHttpMessageConverter() {
// 1.需要意義一個 FastJsonHttpMessageConverter荒吏,比如是否格式化返回json
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加 fastJson的配置信息, 比如是否要格式化返回 json 數(shù)據(jù)
FastJsonConfig jsonConfig = new FastJsonConfig();
jsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(jsonConfig);
// 4.返回一個 HttpMessageConverters
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
3.2.3 簡單的應用
我們?nèi)バ薷奈覀兊腏SONDemo類
1.序列化date的format
/**
* 通過@JSONField來設置date序列化的格式
* format="yyyy-MM-dd HH:mm","yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"
*/
@JSONField(format="yyyy-MM-dd HH:mm")
private Date createTime;
2.過濾某些無用字段
/**
* 通過@JOSNFiled標簽的serialize來設置是否需要序列化
* serialize=false
*/
@JSONField(serialize=false)
private String remark;
JSONDemo完整代碼
package com.avalanching.spring_hellow.model;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
public class JSONDemo {
private Integer id;
private String name;
/**
* 通過@JOSNFiled標簽的serialize來設置是否需要序列化
* serialize=false
*/
@JSONField(serialize=false)
private String remark;
/**
* 通過@JSONField來設置date序列化的格式
* format="yyyy-MM-dd HH:mm","yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"
*/
@JSONField(format="yyyy-MM-dd HH:mm")
private Date createTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
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;
}
}
啟動工程渊鞋,等啟動完畢打開瀏覽器輸入地址:http://localhost:8080/jsondemo
4.spring-boot的熱部署
spring-boot-devtools實現(xiàn)熱部署
spring-boot-devtools
1.是一個為開發(fā)者服務的一個模塊绰更,其中最重要的功能就是自動應用代碼更改到最新的App上面去瞧挤。原理是在發(fā)現(xiàn)代碼有更改之后,重新啟動應用儡湾,但是速度比手動停止后再啟動還要更快特恬,更快指的不是節(jié)省出來的手工操作的時間。
2.其深層原理是使用了兩個ClassLoader徐钠,一個Classloader加載那些不會改變的類(第三方Jar包)癌刽,另一個ClassLoader加載會更改的類,稱為 restart ClassLoader
3.這樣在有代碼更改的時候丹皱,原來的restart ClassLoader 被丟棄妒穴,重新創(chuàng)建一個restart ClassLoader,由于需要加載的類相比較少摊崭,所以實現(xiàn)了較快的重啟時間(5秒以內(nèi))讼油。
前往pom.xml文件添加spring-boot-devtools的依賴
在<dependencies></dependencies>中添加
<!-- spring-boot 熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
在<project></project>之中添加<build></build>節(jié)點,如果存在了<plugins></plugins>節(jié)點呢簸,如果<plugins></plugins>存在矮台,只需添加如下代碼:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果沒有該項配置,肯呢個devtools不會起作用根时,即應用不會restart -->
<fork>true</fork>
</configuration>
</plugin>
完整節(jié)點如下:
在<project></project>之間
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果沒有該項配置瘦赫,肯呢個devtools不會起作用,即應用不會restart -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
配置完成蛤迎,保存pom.xml文件确虱,只需要保存文件,自動重啟服務器
如果配置不成功:
1.檢查是否正確導入了spring-boot-devtools的jar包
2.查看<fork>true</fork>是否配置了
3.查看版本替裆,不同版本不同配置方式
注意:電腦內(nèi)存不足的校辩,請不要使用這種方式,老老實實重新啟動