Result

import com.alibaba.fastjson.JSON; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; /** * @author Zoctan * @date 2018/07/15 */ @ApiModel(value = "响应结果") public class Result {   @ApiModelProperty(value = "状态码")   private Integer code;   @ApiModelProperty(value = "消息")   private String message;   @ApiModelProperty(value = "数据")   private T data;   @Override   public String toString() {     return JSON.toJSONString(this);   }   public Integer getCode() {     return this.code;   }   public Result setCode(final Integer code) {     this.code = code;     return this;   }   public String getMessage() {     return this.message;   }   public Result setMessage(final String message) {     this.message = message;     return this;   }   public T getData() {     return this.data;   }   public Result setData(final T data) {     this.data = data;     return this;   } }




/** * 响应状态码枚举类 * *

自定义业务异常 2*** 开始 * *

原有类异常 4*** 开始 * * @author Zoctan * @date 2018/07/14 */ public enum ResultCode {   SUCCEED_REQUEST_FAILED_RESULT(1000, "成功请求,但结果不是期望的成功结果"),   FIND_FAILED(2000, "查询失败"),   SAVE_FAILED(2001, "保存失败"),   UPDATE_FAILED(2002, "更新失败"),   DELETE_FAILED(2003, "删除失败"),   DUPLICATE_NAME(2004, "账户名重复"),   DATABASE_EXCEPTION(4001, "数据库异常"),   UNAUTHORIZED_EXCEPTION(4002, "认证异常"),   VIOLATION_EXCEPTION(4003, "验证异常");   private final int value;   private final String reason;   ResultCode(final int value, final String reason) {     this.value = value;     this.reason = reason;   }   public int getValue() {     return this.value;   }   public String getReason() {     return this.reason;   }   public String format(final Object... objects) {     return objects.length > 0 ? String.format(this.getReason(), objects) : this.getReason();   } }




package com.dh.core.response; import org.springframework.http.HttpStatus; /** * 响应结果生成工具 * * @author Zoctan * @date 2018/06/09 */ public class ResultGenerator {   /**   * 成功响应结果   *   * @param data 内容   * @return 响应结果   */   public static Result genOkResult(final T data) {     return new Result().setCode(HttpStatus.OK.value()).setData(data);   }   /**   * 成功响应结果   *   * @return 响应结果   */   public static Result genOkResult() {     return genOkResult(null);   }   /**   * 失败响应结果   *   * @param code 状态码   * @param message 消息   * @return 响应结果   */   public static Result genFailedResult(final int code, final String message) {     return new Result().setCode(code).setMessage(message);   }   /**   * 失败响应结果   *   * @param resultCode 状态码枚举   * @param message 消息   * @return 响应结果   */   public static Result genFailedResult(final ResultCode resultCode, final String message) {     return genFailedResult(resultCode.getValue(), message);   }   /**   * 失败响应结果   *   * @param resultCode 状态码枚举   * @return 响应结果   */   public static Result genFailedResult(final ResultCode resultCode) {     return genFailedResult(resultCode.getValue(), resultCode.getReason());   }   /**   * 失败响应结果   *   * @param message 消息   * @return 响应结果   */   public static Result genFailedResult(final String message) {     return genFailedResult(ResultCode.SUCCEED_REQUEST_FAILED_RESULT.getValue(), message);   }   /**   * 失败响应结果   *   * @return 响应结果   */   public static Result genFailedResult() {     return genFailedResult(ResultCode.SUCCEED_REQUEST_FAILED_RESULT);   } }





@PutMapping   public Result update(@RequestBody final RoleDTO roleDTO) {     this.roleService.update(roleDTO);     return ResultGenerator.genOkResult();   }   @GetMapping("/{id}")   public Result detail(@PathVariable final Long id) {     final RoleDO role = this.roleService.getById(id);     return ResultGenerator.genOkResult(role);   }





@GetMapping   public Result list(       @RequestParam(defaultValue = "0") final Integer page,       @RequestParam(defaultValue = "0") final Integer size) {     PageHelper.startPage(page, size);     final List list = this.roleService.findAll();     final PageInfo pageInfo = new PageInfo<>(list);     return ResultGenerator.genOkResult(pageInfo);   }

你可能感兴趣的:(Result)