spring boot 統(tǒng)一JSON格式的接口返回結(jié)果的實(shí)現(xiàn)
前后端分離的項(xiàng)目開(kāi)發(fā)前,會(huì)提前規(guī)定好數(shù)據(jù)返回格式,本文以JSON為例。
第一步,定義好JavaBean。
package com.yclouds.myhelper.web.response;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yclouds.myhelper.web.error.code.BaseEnumError;
import java.io.Serializable;
import lombok.Data;
/**
* 所有服務(wù)統(tǒng)一響應(yīng)數(shù)據(jù)格式
*
* @author ye17186
* @version 2019/2/15 14:40
*/
@Data
public class ApiResp<T> implements Serializable {
private static final long serialVersionUID = 9211889136173018364L;
/**
* 正常響應(yīng)碼
*/
private static final int SUCCESS_CODE = 0;
/**
* 正常響應(yīng)消息
*/
private static final String SUCCESS_MSG = "SUCCESS";
/**
* 錯(cuò)誤碼
*/
private int code = SUCCESS_CODE;
/**
* 錯(cuò)誤信息
*/
private String msg = SUCCESS_MSG;
/**
* 響應(yīng)內(nèi)容,默認(rèn)為null
*/
private T data = null;
/**
* 是否的正常響應(yīng)
*
* @return true=正常;false=異常
*/
@JsonIgnore
public boolean isOK() {
return code == SUCCESS_CODE;
}
/**
* 無(wú)data的正常返回
*/
public static ApiResp retOK() {
return new ApiResp();
}
/**
* 有data的正常返回
*
* @param data data內(nèi)容
* @param <T> data類(lèi)型
*/
public static <T> ApiResp<T> retOK(T data) {
ApiResp<T> response = new ApiResp<>();
response.setData(data);
return response;
}
/**
* 無(wú)data的失敗返回
*
* @param error 錯(cuò)誤類(lèi)型
*/
public static ApiResp retFail(BaseEnumError error) {
return retFail(error.getCode(), error.getMsg());
}
/**
* 有data的失敗返回
*
* @param error 錯(cuò)誤類(lèi)型
* @param data 詳細(xì)錯(cuò)誤信息
*/
public static <T> ApiResp<T> retFail(BaseEnumError error, T data) {
return retFail(error.getCode(), error.getMsg(), data);
}
/**
* 無(wú)data的失敗返回
*
* @param code 錯(cuò)誤碼
* @param msg 錯(cuò)誤信息
*/
public static <T> ApiResp<T> retFail(int code, String msg) {
ApiResp<T> response = new ApiResp<>();
response.setCode(code);
response.setMsg(msg);
return response;
}
/**
* 有data的失敗返回
* <br>
* 失敗返回的場(chǎng)景不多,所以沒(méi)有嚴(yán)格要求T泛型
*
* @param code 錯(cuò)誤碼
* @param msg 錯(cuò)誤信息
*/
public static <T> ApiResp<T> retFail(int code, String msg, T data) {
ApiResp<T> response = new ApiResp<>();
response.setCode(code);
response.setMsg(msg);
response.setData(data);
return response;
}
}
第二步,在Controller中使用
@GetMapping("/test1")
public ApiResp test1() {
return ApiResp.retOK("Hello World");
}
@GetMapping("/test2")
public ApiResp test2() {
return ApiResp.retFail(BaseEnumError.SYSTEM_NO_LOGIN);
}
可以在Postman查看具體的響應(yīng)內(nèi)容如下:


當(dāng)然,ApiResp中的泛型,也可以使用復(fù)雜的數(shù)據(jù)對(duì)象。實(shí)際開(kāi)發(fā)中,每種錯(cuò)誤響應(yīng)都有自己的響應(yīng)碼code,和錯(cuò)誤信息msg,示例中統(tǒng)一定義了一個(gè)BaseEnumError的枚舉類(lèi),具體內(nèi)容可參照我的github項(xiàng)目https://github.com/ye17186/myhelper-spring-boot-starter
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatisplus解除分頁(yè)限制的實(shí)現(xiàn)
這篇文章主要介紹了mybatisplus解除分頁(yè)限制的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
SpringCloud Finchley Gateway 緩存請(qǐng)求Body和Form表單的實(shí)現(xiàn)
在接入Spring-Cloud-Gateway時(shí),可能有需求進(jìn)行緩存Json-Body數(shù)據(jù)或者Form-Urlencoded數(shù)據(jù)的情況。這篇文章主要介紹了SpringCloud Finchley Gateway 緩存請(qǐng)求Body和Form表單的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2019-01-01
Java源碼解析HashMap的tableSizeFor函數(shù)
今天小編就為大家分享一篇關(guān)于Java源碼解析HashMap的tableSizeFor函數(shù),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
Java類(lèi)的訪(fǎng)問(wèn)權(quán)限關(guān)鍵字用法說(shuō)明
這篇文章主要介紹了Java類(lèi)的訪(fǎng)問(wèn)權(quán)限關(guān)鍵字用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
Spring Boot自定義配置實(shí)現(xiàn)IDE自動(dòng)提示功能
這篇文章主要介紹了Spring Boot自定義配置實(shí)現(xiàn)IDE自動(dòng)提示功能,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08

