多平台统一管理软件接口,如何实现多平台统一管理软件接口
300
2022-11-25
Java封装统一的Result Model案例
在开发过程中,有时候会需要使用错误码+错误信息的形式,来返回某些业务操作的错误结果信息,来代替效率较低的异常传递。
这样就需要封装一个统一的Result model作为返回值,代替直接返回数据等结果。
1.定义错误码 - 错误信息接口
错误码可能是String、Integer、Long 等类型,也可能是enum类型。因此这里使用泛型来代替,错误码类型,可以提高灵活性
。也可以通过枚举直接实现CodeMessage接口。
public interface CodeMessage
C getCode();
String getMessage();
}
2.实现Result Model
这里定义的是service层的Result。有时候在controller层只会作一些简单的参数校验,在service层会作进一步的校验,
如果controller中需要统一返回一个jsonResult可以将ServiceResult作一个简单的转换器进行转换。
public class ServiceResult
public static final CodeMessage
private T data;
private CodeMessage
private boolean isSuccess;
ServiceResult(T data, boolean isSuccess, CodeMessage
this.data = data;
this.message = message;
this.isSuccess = isSuccess;
}
public T getData() {
return data;
}
public CodeMessage
return message;
}
public boolean isSuccess() {
return isSuccess;
}
public static
ServiceResultBuilder
return builder.isSuccess(true);
}
public static
ServiceResultBuilder
return builder.isSuccess(true).code(codeMessage.getCode()).message(codeMessage.getMessage());
}
public static
ServiceResultBuilder
return success.data(data).build();
}
public static <D, C> ServiceResult
ServiceResultBuilder
return builder.isSuccess(false).code(codeMessage.getCode()).message(codeMessage.getMessage()).build();
}
public static
ServiceResultBuilder
return builder.isSuccess(false);
}
static
return new ServiceResultBuilder<>();
}
public static class DefaultMessage
private C code;
private String message;
public DefaultMessage(C code, String message) {
this.code = code;
this.message = message;
}
@Override
public C getCode() {
return code;
}
@Override
public String getMessage() {
return message;
}
@Override
public String toString() {
return "DefaultMessage{" +
"code=" + code +
", message='" + message + '\'' +
'}';
}
}
public static class ServiceResultBuilder
private T data;
private C code;
private String message;
private boolean isSuccess;
ServiceResultBuilder() { //package private
}
public ServiceResultBuilder
this.data = data;
return this;
}
ServiceResultBuilder
this.isSuccess = isSuccess;
return this;
}
public ServiceResultBuilder
this.code = code;
return this;
}
public ServiceResultBuilder
this.message = message;
return this;
}
public ServiceResult
Objects.requireNonNull(code, "code");
Objects.requireNonNull(code, "message");
return new ServiceResult<>(data, isSuccess, new DefaultMessage<>(code, message));
}
}
@Override
public String toString() {
return "ServiceResult{" +
"data=" + data +
", message=" + message +
", isSuccess=" + isSuccess +
'}';
}
}
3.使用
定义枚举错误码
pubilc enum CodeMessageEnum implements CodeMessage
some_error(1001, "错误信息。");
Integer code;
String message;
CodeMessageEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
@Override
public Integer getCode() {
return code;
}
@Override
public String getMessage() {
return message;
}
}
//返回正确结果带返回值。
ServiceResult, String> s = ServiceResult.success(Arrays.asList("1", "2", "3"));
s.isSuccess(); //true
ServiceResult, Integer> e1 = ServiceResult.error(CodeMessageEnum.some_error);
e1.isSuccess(); //false
ServiceResult, Long> e2 = ServiceResult.error(new ServiceResult.DefaultMessage<>(1000L, "error"));
e2.isSuccess(); //false
//ServiceResult{data=[1, 2, 3], message=DefaultMessage{code=00000000, message='success'}, isSuccess=true}
System.out.println(s);
//ServiceResult{data=null, message=DefaultMessage{code=1001, message='错误信息。'}, isSuccess=false}
System.out.println(e1);
//ServiceResult{data=null, message=DefaultMessage{code=1000, message='error'}, isSuccess=false}
System.out.println(e2);
补充知识:java 封装返回结果实体类 返回结果以及错误信息
我就废话不多说了,大家还是直接看代码吧~
public class ResponseMessage {
private final static String STATUS_OK = "0";
private final static String STATUS_ERROR = "1";
private String status; // 状态
private Object data; // 返回值
private Exception e; // 异常类捕获
private String msg; // 自定义信息
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Exception getE() {
return e;
}
public void setE(Exception e) {
this.e = e;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public ResponseMessage() {
super();
}
public ResponseMessage(String status, Object data, Exception e, String msg) {
super();
this.status = status;
this.data = data;
this.e = e;
this.msg = msg;
}
/**
* 成功的结果
* @param data 返回结果
* @param msg 返回信息
*/
public static ResponseMessage newOkInstance(Object data, String msg) {
return new ResponseMessage(ResponseMessage.STATUS_OK, data, null, msg);
}
/**
* 成功的结果
* @param data 返回结果
*/
public ResponseMessage newOkInstance(Object data) {
return new ResponseMessage(ResponseMessage.STATUS_OK, data, null, null);
}
/**
* 失败的结果
* @param msg 返回信息
*/
public static ResponseMessage newErrorInstance(String msg) {
return new ResponseMessage(ResponseMessage.STATUS_ERROR, null, null, msg);
}
/**
* 失败的结果
* @param e 异常对象
* @param msg 返回信息
*/
public static ResponseMessage newErrorInstance(Exception e, String msg) {
return new ResponseMessage(ResponseMessage.STATUS_ERROR, null, e, msg);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~