調(diào)用后端服務(wù)后的返回結(jié)果有多種類型,如:String
,Integer
,Boolean
,List
,Map
等,在一個(gè)項(xiàng)目中,為了保持統(tǒng)一性,我們方法返回結(jié)果可以全部使用JSON
數(shù)據(jù)格式,如下:
{
"code":200,
"msg":"成功",
"data":"JSON數(shù)據(jù)"
}
其中code
是本次請(qǐng)求處理結(jié)果對(duì)應(yīng)的狀態(tài)碼,msg
是狀態(tài)碼對(duì)應(yīng)的解釋信息,data
是要返回的數(shù)據(jù)內(nèi)容,可以是任意一個(gè)對(duì)象。
封裝響應(yīng)信息對(duì)象
public class ResponseEntity<T> implements Serializable {
private static final long serialVersionUID = 3595741978061989861L;
private Integer code;//狀態(tài)碼
private String msg;//狀態(tài)碼對(duì)應(yīng)信息
private T data;//要返回的數(shù)據(jù)
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
狀態(tài)碼,Http
請(qǐng)求狀態(tài)碼有多種,使用枚舉列舉,如下示例:
public enum ResponseEnum {
SUCCESS(200, "成功"),
FAIL(-1, "失敗"),
ERROR_400(400, "錯(cuò)誤的請(qǐng)求"),
ERROR_404(404, "訪問(wèn)資源不存在"),
ERROR_500(500, "服務(wù)器異常");
private Integer code;
private String msg;
ResponseEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
創(chuàng)建一個(gè)公共類,生成響應(yīng)對(duì)象
public class ResponseUtil {
/**
* 成功返回
* @param object 返回?cái)?shù)據(jù)
* @return
*/
public static ResponseEntity success(Object object){
ResponseEntity resp = new ResponseEntity();
resp.setCode(ResponseEnum.SUCCESS.getCode());
resp.setMsg(ResponseEnum.SUCCESS.getMsg());
resp.setData(object);
return resp;
}
/**
* 成功返回 無(wú)數(shù)據(jù)
* @return
*/
public static ResponseEntity success(){
return success(null);
}
/**
* 失敗返回
* @param responseEnum 響應(yīng)標(biāo)識(shí)
* @return
*/
public static ResponseEntity error(ResponseEnum responseEnum){
ResponseEntity resp = new ResponseEntity();
resp.setCode(responseEnum.getCode());
resp.setMsg(responseEnum.getMsg());
return resp;
}
}
Spring
中的控制器可以用 @Controller
和 @RestController
注解來(lái)聲明,其中@Controller
標(biāo)識(shí)當(dāng)前控制器是SpringMvc
的控制器,要返回JSON
對(duì)象數(shù)據(jù),需要和@ResponseBody
注解聯(lián)合使用;@RestController
主要用來(lái)構(gòu)建Restful
風(fēng)格接口,返回客戶端的請(qǐng)求數(shù)據(jù),相當(dāng)于同時(shí)使用@Controller
和@ResponseBody
注解。
(推薦課程:Spring教程)
創(chuàng)建Pojo
包及對(duì)應(yīng)的實(shí)體類
public class DemoEntity {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
創(chuàng)建控制器
@Controller
@Controller
public class DemoController {
@RequestMapping(value = "/users", method= RequestMethod.GET)
@ResponseBody
public ResponseEntity users(){
List<DemoEntity> list = new ArrayList<DemoEntity>();
DemoEntity demo = new DemoEntity();
demo.setId(1);
demo.setName("蝸牛");
list.add(demo);
DemoEntity demo1 = new DemoEntity();
demo1.setId(2);
demo1.setName("葡萄");
list.add(demo1);
return ResponseUtil.success(list);
}
}
測(cè)試:?jiǎn)?dòng)服務(wù)后,在瀏覽器地址欄輸入http://localhost:8080/users
,可以看到頁(yè)面上的輸出信息
{"code":200,"msg":"成功","data":[{"id":1,"name":"蝸牛"},{"id":2,"name":"葡萄"}]}
@RestController
@RestController
public class DemoRestController {
@RequestMapping(value = "/users1", method= RequestMethod.GET)
public ResponseEntity users(){
List<DemoEntity> list = new ArrayList<DemoEntity>();
DemoEntity demo = new DemoEntity();
demo.setId(1);
demo.setName("蝸牛");
list.add(demo);
DemoEntity demo1 = new DemoEntity();
demo1.setId(2);
demo1.setName("葡萄");
list.add(demo1);
return ResponseUtil.success(list);
}
}
請(qǐng)求后也能看到如上的結(jié)果。
(推薦教程:Spring Boot 那些事)
以上就是關(guān)于SpringBoot:統(tǒng)一JSON信息返回的相關(guān)介紹,希望對(duì)大家有所幫助。