image.png

    创建JSONResult类

    1.图灵

    1. package com.lv.utils;
    2. import com.fasterxml.jackson.annotation.JsonIgnore;
    3. import com.fasterxml.jackson.databind.ObjectMapper;
    4. /**
    5. * @author
    6. * @version V1.0
    7. * @ Title: JSONResult.java
    8. * @ Description: 自定义响应数据结构
    9. * 本类可提供给 H5/ios/安卓/公众号/小程序 使用
    10. * 前端接受此类数据(json object)后,可自行根据业务去实现相关功能
    11. * 200:表示成功
    12. * 500:表示错误,错误信息在msg字段中
    13. * 501:bean验证错误,不管多少个错误都以map形式返回
    14. * 502:拦截器拦截到用户token出错
    15. * 555:异常抛出信息
    16. * 556: 用户qq校验异常
    17. * 557:校验用户是否在CAS登录(用户门票校验)
    18. */
    19. public class JSONResult {
    20. // 定义jackson对象
    21. private static final ObjectMapper MAPPER = new ObjectMapper();
    22. // 响应业务状态
    23. private Integer status;
    24. // 响应消息
    25. private String msg;
    26. // 响应中的数据
    27. private Object data;
    28. @JsonIgnore
    29. private String ok; // 不使用
    30. public static JSONResult build(Integer status, String msg, Object data) {
    31. return new JSONResult(status, msg, data);
    32. }
    33. public static JSONResult build(Integer status, String msg, Object data, String ok) {
    34. return new JSONResult(status, msg, data, ok);
    35. }
    36. public static JSONResult ok(Object data) {
    37. return new JSONResult(data);
    38. }
    39. public static JSONResult ok() {
    40. return new JSONResult(null);
    41. }
    42. public static JSONResult errorMsg(String msg) {
    43. return new JSONResult(500, msg, null);
    44. }
    45. public static JSONResult errorMap(Object data) {
    46. return new JSONResult(501, "error", data);
    47. }
    48. public static JSONResult errorTokenMsg(String msg) {
    49. return new JSONResult(502, msg, null);
    50. }
    51. public static JSONResult errorException(String msg) {
    52. return new JSONResult(555, msg, null);
    53. }
    54. public static JSONResult errorUserQQ(String msg) {
    55. return new JSONResult(556, msg, null);
    56. }
    57. public static JSONResult errorUserTicket(String msg) {
    58. return new JSONResult(557, msg, null);
    59. }
    60. public JSONResult() {
    61. }
    62. public JSONResult(Integer status, String msg, Object data) {
    63. this.status = status;
    64. this.msg = msg;
    65. this.data = data;
    66. }
    67. public JSONResult(Integer status, String msg, Object data, String ok) {
    68. this.status = status;
    69. this.msg = msg;
    70. this.data = data;
    71. this.ok = ok;
    72. }
    73. public JSONResult(Object data) {
    74. this.status = 200;
    75. this.msg = "OK";
    76. this.data = data;
    77. }
    78. public Boolean isOK() {
    79. return this.status == 200;
    80. }
    81. public Integer getStatus() {
    82. return status;
    83. }
    84. public void setStatus(Integer status) {
    85. this.status = status;
    86. }
    87. public String getMsg() {
    88. return msg;
    89. }
    90. public void setMsg(String msg) {
    91. this.msg = msg;
    92. }
    93. public Object getData() {
    94. return data;
    95. }
    96. public void setData(Object data) {
    97. this.data = data;
    98. }
    99. public String getOk() {
    100. return ok;
    101. }
    102. public void setOk(String ok) {
    103. this.ok = ok;
    104. }
    105. }

    2.尚硅谷

    1. package com.lv.commonutils;
    2. import io.swagger.annotations.ApiModelProperty;
    3. import lombok.Data;
    4. import java.util.HashMap;
    5. import java.util.Map;
    6. @Data
    7. public class Result {
    8. @ApiModelProperty(value = "是否成功")
    9. private Boolean success;
    10. @ApiModelProperty(value = "返回码")
    11. private Integer code;
    12. @ApiModelProperty(value = "返回消息")
    13. private String message;
    14. @ApiModelProperty(value = "返回数据")
    15. private Map<String, Object> data = new HashMap<String, Object>();
    16. private Result(){}
    17. public static Result ok(){
    18. Result result = new Result();
    19. result.setSuccess(true);
    20. result.setCode(ResultCode.SUCCESS);
    21. result.setMessage("成功");
    22. return result;
    23. }
    24. public static Result error(){
    25. Result result = new Result();
    26. result.setSuccess(false);
    27. result.setCode(ResultCode.ERROR);
    28. result.setMessage("失败");
    29. return result;
    30. }
    31. public Result success(Boolean success){
    32. this.setSuccess(success);
    33. return this;
    34. }
    35. public Result message(String message){
    36. this.setMessage(message);
    37. return this;
    38. }
    39. public Result code(Integer code){
    40. this.setCode(code);
    41. return this;
    42. }
    43. public Result data(String key, Object value){
    44. this.data.put(key, value);
    45. return this;
    46. }
    47. public Result data(Map<String, Object> map){
    48. this.setData(map);
    49. return this;
    50. }
    51. }
    52. package com.lv.commonutils;
    53. public interface ResultCode {
    54. public static Integer SUCCESS = 20000;
    55. public static Integer ERROR = 20001;
    56. }