1. /**
    2. * 接口统一返回包装类
    3. */
    4. @Data
    5. @NoArgsConstructor
    6. @AllArgsConstructor
    7. public class Result {
    8. private String code;
    9. private String msg;
    10. private Object data;
    11. public static Result success() {
    12. return new Result(Constants.CODE_200, "", null);
    13. }
    14. public static Result success(Object data) {
    15. return new Result(Constants.CODE_200, "", data);
    16. }
    17. public static Result error(String code, String msg) {
    18. return new Result(code, msg, null);
    19. }
    20. public static Result error() {
    21. return new Result(Constants.CODE_500, "系统错误", null);
    22. }
    23. }
    1. public interface Constants {
    2. String CODE_200 = "200"; //成功
    3. String CODE_401 = "401"; // 权限不足
    4. String CODE_400 = "400"; // 参数错误
    5. String CODE_500 = "500"; // 系统错误
    6. String CODE_600 = "600"; // 其他业务异常
    7. String DICT_TYPE_ICON = "icon";
    8. }

    control层使用案例

    1. @GetMapping("/example")
    2. public Result get() {
    3. Map<String, Object> map = new HashMap<>();
    4. map.put("x", CollUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
    5. map.put("y", CollUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));
    6. return Result.success(map);
    7. }