状态码类:
public enum ServiceCode {
ERR_UNAUTHORIZED_DISABLED(40110),
ERR_JWT_MALFORMED(60100),
ERR_JWT_SIGNATURE(60200),
ServiceCode(Integer value) {
public Integer getValue() {
返回类:
public class JsonResult implements Serializable {
@ApiModelProperty("业务状态码")
public static JsonResult ok() {
public static JsonResult ok(T data) {
JsonResult jsonResult = new JsonResult();
jsonResult.setState(ServiceCode.OK.getValue());
jsonResult.setData(data);
public static JsonResult fail(ServiceException e) {
return fail(e.getServiceCode(), e.getMessage());
public static JsonResult fail(ServiceCode serviceCode, String message) {
JsonResult jsonResult = new JsonResult();
jsonResult.setState(serviceCode.getValue());
jsonResult.setMessage(message);
public static JsonResult fail(ServiceException e) {
return fail(e.getServiceCode(), e.getMessage());
public static JsonResult fail(ServiceCode serviceCode, String message) {
JsonResult jsonResult = new JsonResult();
jsonResult.setState(serviceCode.getValue());
jsonResult.setMessage(message);

(没有找到数据、不符合业务、查询失败等均抛出异常类,相当于if ···else···中的else情况)异常类:
public class ServiceException extends RuntimeException {
private ServiceCode serviceCode;
public ServiceException(ServiceCode serviceCode, String message) {
this.serviceCode = serviceCode;
public ServiceCode getServiceCode() {
使用:
public JsonResult 方法名(参数(可选))
public JsonResult 方法名(参数(可选))
JsonResult jsonResult = JsonResult.fail(ServiceCode.状态码, message);
throw new ServiceException(ServiceCode.状态码, message);