前後端分離Java統一響應返回格式

專注的阿熊發表於2021-01-07

ResultCode

public  class ResultCode {

    public static Integer SUCCESS = 200;

    public static Integer ERROR = 500;

    public static Integer FORBIDDEN = 403;

    public static Integer UNAUTHORIZED = 401;}

Result

@Data //lombok 註解 public class Result {

    // 返回碼

    private Integer code;

    // 返回訊息

    private String message;

    // 返回資料

    private Map<String, Object> data = new HashMap<String, Object>();

    public Result(){跟單網}

    public Result message(String message){

        this.setMessage(message);

        return this;

    public Result code(Integer code){

        this.setCode(code);

        return this;

    public Result data(String key, Object value){

        this.data.put(key, value);

        return this;

    }public Result data(Map<String, Object> map){

        this.setData(map);

        return this;

使用示例

@RequestMapping("/get")

    public Result get(){

        HashMap map = new HashMap();

        map.put("username","admin");

        map.put("password","123456");

        Result r = new Result();

        r.code(ResultCode.SUCCESS).message(" 成功訪問 ").data(map);

        return r;

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2748128/,如需轉載,請註明出處,否則將追究法律責任。

相關文章