App下載

SpringBoot:統(tǒng)一JSON信息返回

猿友 2020-08-18 16:20:57 瀏覽數(shù) (3183)
反饋

調用后端服務后的返回結果有多種類型,如:String,Integer,Boolean,List,Map等,在一個項目中,為了保持統(tǒng)一性,我們方法返回結果可以全部使用JSON數(shù)據格式,如下:

{
    "code":200,
    "msg":"成功",
    "data":"JSON數(shù)據"
}

其中code 是本次請求處理結果對應的狀態(tài)碼,msg是狀態(tài)碼對應的解釋信息,data是要返回的數(shù)據內容,可以是任意一個對象。

封裝響應信息對象

public class ResponseEntity<T> implements Serializable {


    private static final long serialVersionUID = 3595741978061989861L;
    private Integer code;//狀態(tài)碼
    private String msg;//狀態(tài)碼對應信息
    private T data;//要返回的數(shù)據


    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請求狀態(tài)碼有多種,使用枚舉列舉,如下示例:

public enum ResponseEnum {


    SUCCESS(200, "成功"),
    FAIL(-1, "失敗"),
    ERROR_400(400, "錯誤的請求"),
    ERROR_404(404, "訪問資源不存在"),
    ERROR_500(500, "服務器異常");


    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)建一個公共類,生成響應對象

public class ResponseUtil {


    /**
     * 成功返回
     * @param object 返回數(shù)據
     * @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;
    }


    /**
     * 成功返回  無數(shù)據
     * @return
     */
    public static ResponseEntity success(){
        return success(null);
    }


    /**
     * 失敗返回
     * @param responseEnum 響應標識
     * @return
     */
    public static ResponseEntity error(ResponseEnum responseEnum){
        ResponseEntity resp = new ResponseEntity();
        resp.setCode(responseEnum.getCode());
        resp.setMsg(responseEnum.getMsg());
        return resp;
    }
}

Spring中的控制器可以用 @Controller@RestController注解來聲明,其中@Controller標識當前控制器是SpringMvc的控制器,要返回JSON對象數(shù)據,需要和@ResponseBody注解聯(lián)合使用;@RestController主要用來構建Restful風格接口,返回客戶端的請求數(shù)據,相當于同時使用@Controller@ResponseBody注解。

(推薦課程:Spring教程

創(chuàng)建Pojo包及對應的實體類

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);
    }
}

測試:啟動服務后,在瀏覽器地址欄輸入http://localhost:8080/users,可以看到頁面上的輸出信息

{"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);
    }
}

請求后也能看到如上的結果。

(推薦教程:Spring Boot 那些事)

以上就是關于SpringBoot:統(tǒng)一JSON信息返回的相關介紹,希望對大家有所幫助。

0 人點贊