12.HTTP结果封装

在demo-core模块下新建HttpStatus枚举类

package com.demo.core.http;

/**
 * 枚举异常
 * Description: 
* date: 2020/5/5 13:29
* * @author lvxk
* @since JDK 1.8 */ public enum HttpStatus { SERVER_SC_INTERNAL_SERVER_ERROR (500,"未知异常,请联系管理员"), SERVER_SUCCESS (200,"SUCCESS"); private Integer code; private String msg; HttpStatus(Integer code, String msg) { this.code = code; this.msg = msg; } public Integer getCode() { return code; } public String getMsg() { return msg; } }

新建HttpResult类

package com.demo.core.http;

import lombok.Data;

/**
 * HttpResult结果封装
 * Description: 
* date: 2020/5/5 13:26
* * @author lvxk
* @since JDK 1.8 */ @Data public class HttpResult { private int code = 200; private String msg; private Object data; public static HttpResult error(){ return error(HttpStatus.SC_INTERNAL_SERVER_ERROR); } public static HttpResult error(HttpStatus httpStatus){ HttpResult httpResult = new HttpResult(); httpResult.setCode(httpResult.getCode()); httpResult.setMsg(httpStatus.getMsg()); return httpResult; } public static HttpResult ok(){ return new HttpResult(); } public static HttpResult ok(Object data){ HttpResult httpResult = new HttpResult(); httpResult.setMsg(HttpStatus.SERVER_SUCCESS.getMsg()); httpResult.setCode(HttpStatus.SERVER_SUCCESS.getCode()); httpResult.setData(data); return httpResult; } }

可以加入单例模式实现链式编程调用

你可能感兴趣的:(12.HTTP结果封装)