/** * define the common response body * * @author lazycece */ @Data @AllArgsConstructor @NoArgsConstructor publicclassResponseData<T> {
/** * the response code */ private Integer status; /** * the response message */ private String message; /** * the response body */ private T body;
/** * define success response data by default code and message, but no body * * @return response */ publicstatic ResponseData success(){ returnnew ResponseData<>(ResponseCode.SUCCESS, ResponseMsg.SUCCESS, null); }
/** * define success response data by default code and message, the body is flexible * * @param body body * @param <T> T * @return response */ publicstatic <T> ResponseData<T> success(T body){ returnnew ResponseData<>(ResponseCode.SUCCESS, ResponseMsg.SUCCESS, body); }
/** * define the fail response by default code and message * * @return response */ publicstatic ResponseData fail(){ returnnew ResponseData<>(ResponseCode.FAIL, ResponseMsg.FAIL, null); }
/** * define the fail response by default code, and the message is flexible * * @param message fail message * @return response */ publicstatic ResponseData fail(String message){ returnnew ResponseData<>(ResponseCode.FAIL, message, null); }
/** * define the fail response, the code and message both flexible * * @param status code * @param message message * @return response */ publicstatic ResponseData fail(Integer status, String message){ returnnew ResponseData<>(status, message, null); } }
@GetMapping("/data/user/info") public ResponseData getDataJson(){ return ResponseData.success(getUserInfo()); }
@GetMapping("/map/user/info") public ResponseMap getMapJson(){ return ResponseMap.success().putting("data", getUserInfo()); }
private User getUserInfo(){ User user = new User(); user.setId("sfjsoop1jsjisf_1"); user.setUsername("lazycece"); user.setName("W"); user.setAge(200); return user; } }