In this chapter, we encapsulate the request result into a unified format. If you have any questions, please contact me at [email protected]. Ask for directions of various gods, thank you

One: What are the benefits?

We call the service returns the type of the String, List, Map, Bean, Int, Boolean type, such as can unify into the result object returned, convenient project team other members call interface

Two: define response code enumeration

package com.example.demo.core.ret; /** * @Description: Enumeration of response codes, * @author * @date 2018/4/19 09:42 */ public enum RetCode {// SUCCESS(200), // failure (400), // Unauthenticated (a signature error) UNAUTHORIZED(401), // The interface is not found NOT_FOUND(404), // Internal server error INTERNAL_SERVER_ERROR(500); public int code; RetCode(int code) { this.code = code; }}Copy the code

Here is a brief list of a few, you can according to the actual business requirements of their own definition

Three: create return object entities

package com.example.demo.core.ret; import java.io.Serializable; import com.alibaba.fastjson.JSON; Public class RetResult<T> {public int code; private String msg; private T data; public RetResult<T>setCode(RetCode retCode) {
      this.code = retCode.code;
      return this;
   }

   public int getCode() {
      return code;
   }

   public RetResult<T> setCode(int code) {
      this.code = code;
      return this;
   }

   public String getMsg() {
      return msg;
   }

   public RetResult<T> setMsg(String msg) {
      this.msg = msg;
      return this;
   }

   public T getData() {
      return data;
   }

   public RetResult<T> setData(T data) {
      this.data = data;
      returnthis; }}Copy the code

Note: code is the status code, MSG is the prompt message, and data is the returned data

Four: data conversion format

package com.example.demo.core.ret; /** * @Description: Public class RetResponse {private final static String SUCCESS = public class RetResponse {private final static String SUCCESS ="success";

   public static <T> RetResult<T> makeOKRsp() {
      return new RetResult<T>().setCode(RetCode.SUCCESS).setMsg(SUCCESS);
   }

   public static <T> RetResult<T> makeOKRsp(T data) {
      return new RetResult<T>().setCode(RetCode.SUCCESS).setMsg(SUCCESS).setData(data);
   }

   public static <T> RetResult<T> makeErrRsp(String message) {
      return new RetResult<T>().setCode(RetCode.FAIL).setMsg(SUCCESS);
   }

   public static <T> RetResult<T> makeRsp(int code, String msg) {
      return new RetResult<T>().setCode(code).setMsg(msg);
   }
   
   public static <T> RetResult<T> makeRsp(int code, String msg, T data) {
      returnnew RetResult<T>().setCode(code).setMsg(msg).setData(data); }}Copy the code

Five: functional testing

Modify the Controller code

Pre-modification code

@PostMapping("/selectById")
public UserInfo selectById(Integer id){
    return userInfoService.selectById(id);
}Copy the code

Request to return data format before transformation

{
    "id": 1,
    "userName": "1"
}Copy the code

Modified code

@PostMapping("/selectById")
public RetResult<UserInfo> selectById(Integer id){
    UserInfo userInfo = userInfoService.selectById(id);
    return RetResponse.makeOKRsp(userInfo);
}Copy the code

Request to return data format after modification

{
    "code": 200,
    "msg": "success"."data": {
        "id": 1,
        "userName": "1"}}Copy the code

How is it? Does it feel cleaner

The project address

Code cloud address: gitee.com/beany/mySpr…

GitHub address: github.com/MyBeany/myS…

Writing articles is not easy, if it is helpful to you, please help click star

At the end

Springboot has wrapped the request results into a unified format, and the subsequent functions will be updated successively. If you have any questions, please contact me at [email protected]. Ask for directions from various gods, thank you.