Result dubbo接口返回类型的设计

 dubbo实现了分布式远程调用的框架,集成了spring配置功能,dubbo要求远程接口的参数和返回值都是可序列化的,Result<T>泛型返回类型可作为接口的统一返回类型:
  • int code,接口调用成功=0,错误码=其他值
  • T object,具体返回值
  • String error,字符串错误码,可选
  • String message,错误消息,可选,error=message可以配置成属性文件
  • Exception exception,异常消息,可选
public class Result<T> implements Serializable {
	/**
	 * 接口调用成功,不需要返回对象
	 */
	public static <T> Result<T> newSuccess(){
		Result<T> result = new Result<>();
		return result;
	}
	
	/**
	 * 接口调用成功,有返回对象
	 */
	public static <T> Result<T> newSuccess(T object) {
		Result<T> result = new Result<>();
		result.setObject(object);
		return result;
	}
	
	/**
	 * 接口调用失败,有错误码和描述,没有返回对象
	 */
	public static <T> Result<T> newFailure(int code, String message){
		Result<T> result = new Result<>();
		result.setCode(code!=0 ? code : -1);
		result.setMessage(message);
		return result;
	}
	
	/**
	 * 接口调用失败,有错误字符串码和描述,没有返回对象
	 */
	public static <T> Result<T> newFailure(String error, String message){
		Result<T> result = new Result<>();
		result.setCode(-1);
		result.setError(error);
		result.setMessage(message);
		return result;
	}
	
	/**
	 * 转换或复制错误结果
	 */
	public static <T> Result<T> newFailure(Result<?> failure){
		Result<T> result = new Result<>();
		result.setCode(failure.getCode()!=0 ? failure.getCode() : -1);
		result.setError(failure.getError());
		result.setMessage(failure.getMessage());
		result.setException(failure.getException());
		return result;
	}
	
	/**
	 * 接口调用失败,返回异常信息
	 */
	public static <T> Result<T> newException(Exception e){
		Result<T> result = new Result<>();
		result.setCode(-1);
		result.setException(e);
		result.setMessage(e.getMessage());
		return result;
	}
	
	private int code;
	private T object;
	private String error;
	private String message;
	private Exception exception;
	
	/** 判断返回结果是否成功 */
	public boolean success() {
		return code == 0;
	}
	/** 判断返回结果是否有结果对象 */
	public boolean hasObject() {
		return code==0 && object!=null;
	}
	/** 判断返回结果是否有异常 */
	public boolean hasException() {
		return exception != null;
	}
	
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public T getObject() {
		return object;
	}
	public void setObject(T object) {
		this.object = object;
	}
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Exception getException() {
		return exception;
	}
	public void setException(Exception exception) {
		this.exception = exception;
	}

	public String toString() {
		StringBuilder result = new StringBuilder("Result");
		if(object!=null) result.append("<"+object.getClass().getSimpleName()+">");
		result.append(": {code="+code);
		if(object!=null) result.append(", object="+object);
		if(error!=null) result.append(", error="+error);
		if(message!=null) result.append(", message="+message);
		if(exception!=null) {
			StringWriter stringWriter = new StringWriter();
			exception.printStackTrace(new PrintWriter(stringWriter));
			result.append(", exception="+stringWriter.toString());
		}
		result.append(" }");
		return result.toString();
	}
}




[ 非英文:245, 总字符:3032 ]
评论列表
  • # 0 ccc 2015-09-19 13:16:02

    哪有要求返回值都要序列化,你可以看dubbo test里面的。。。。。。package com.alibaba.dubbo.rpc.proxy;

    /*** TestService*/

    public interface DemoService{void sayHello(String name);

    String echo(String text);
    
    long timestamp();
    
    String getThreadName();
    
    int getSize(String[] strs);
    
    int getSize(Object[] os);
    
    Object invoke(String service, String method) throws Exception;
    
    int stringLength(String str);
    
    Type enumlength(Type... types);
    

    }

  • # 1 hongwei 2015-10-30 13:42:25

    返回值和接口参数,需要在client和server之间传递,总会有序列化和编码解码。int、String、Enum、Object、void等或许还可以,你试试自定义User对象并且不实现序列化接口(最好再加个不可序列化的字段)。另外这里是封装的通用返回类型,要考虑到:返回值有多种类型,错误码和错误消息,异常返回等。还有一个AopLogger可以对所有的dubbo远程调用做切面,详细地记录下调用和返回结果的日志。

  • # 2 hongwei 2015-10-30 14:22:46

    dubbo返回结果不序列化会是怎样,https://www.xlongwei.com/detail/15103014

  

      
    • 0
      点赞
    • 2
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值