随笔Java web 返回参数模板 Response

Posted 醇氧

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随笔Java web 返回参数模板 Response相关的知识,希望对你有一定的参考价值。

Java web 返回参数模板 Response
public class Response<T> 
    private int version = 0;
    private int status = 0;
    private String errMsg = "";
    /** @deprecated */
    @Deprecated
    private String errorMsg = "";
    private long ts = System.currentTimeMillis();
    private T data;

    public Response() 
    

    public static <T> ResponseBuilder<T> builder() 
        return new ResponseBuilder();
    

    public static <T> Response<T> ok() 
        return ok((Object)null);
    

    public static <T> Response<T> ok(T data) 
        return ok(0, data);
    

    public static <T> Response<T> ok(int version, T data) 
        return ok(version, System.currentTimeMillis(), data);
    

    public static <T> Response<T> ok(int version, long ts, T data) 
        return of(ErrorCode.OK, version, ts, "ok", data);
    

    public void setErrorCode(ErrorCode code) 
        this.setErrorCode(code, code.getMsg());
    

    public void setErrorCode(ErrorCode code, String msg) 
        if (null != code) 
            this.status = code.getCode();
            this.errMsg = msg;
            this.errorMsg = msg;
        

    

    public static <T> Response<T> error(ErrorCode code) 
        return of(code, code.getMsg());
    

    public static <T> Response<T> error(ErrorCode code, T data) 
        return of(code, (String)null, data);
    

    public static Response<String> error(String data, ErrorCode code) 
        return of(code, (String)null, data);
    

    public static <T> Response<T> error(ErrorCode code, String msg) 
        return of(code, msg, (Object)null);
    

    public static <T> Response<T> error(ErrorCode code, String msg, T data) 
        return of(code, 0, msg, data);
    

    public static <T> Response<T> error(ErrorCode code, int version, String msg, T data) 
        return of(code, version, System.currentTimeMillis(), msg, data);
    

    public static <T> Response<T> error(ErrorCode code, int version, long ts, String msg, T data) 
        return of(code, version, ts, msg, data);
    

    public static <T> Response<T> of(ErrorCode code) 
        return of(code, code.getMsg());
    

    public static <T> Response<T> of(ErrorCode code, T data) 
        return of(code, (String)null, data);
    

    public static Response<String> of(String data, ErrorCode code) 
        return of(code, (String)null, data);
    

    public static <T> Response<T> of(ErrorCode code, String msg) 
        return of(code, msg, (Object)null);
    

    public static <T> Response<T> of(ErrorCode code, String msg, T data) 
        return of(code, 0, msg, data);
    

    public static <T> Response<T> of(ErrorCode code, int version, String msg, T data) 
        return of(code, version, System.currentTimeMillis(), msg, data);
    

    public static <T> Response<T> of(ErrorCode code, int version, long ts, String msg, T data) 
        Response<T> resp = new Response();
        resp.setErrorCode(code);
        resp.setData(data);
        if (null != msg) 
            resp.setErrMsg(msg);
        

        resp.setVersion(version);
        resp.setTs(ts);
        return resp;
    

    public int getStatus() 
        return this.status;
    

    public void setStatus(int status) 
        this.status = status;
    

    public String getErrMsg() 
        return this.errMsg;
    

    public void setErrMsg(String errMsg) 
        this.errMsg = errMsg;
        this.errorMsg = errMsg;
    

    public T getData() 
        return this.data;
    

    public void setData(T data) 
        this.data = data;
    

    /** @deprecated */
    @Deprecated
    public String getErrorMsg() 
        return this.errorMsg;
    

    /** @deprecated */
    @Deprecated
    public void setErrorMsg(String errorMsg) 
        this.errMsg = errorMsg;
        this.errorMsg = errorMsg;
    

    public int getVersion() 
        return this.version;
    

    public void setVersion(int version) 
        this.version = version;
    

    public long getTs() 
        return this.ts;
    

    public void setTs(long ts) 
        this.ts = ts;
    

    public String toString() 
        return "Responseversion=" + this.version + ", status=" + this.status + ", errMsg='" + this.errMsg + '\\'' + ", errorMsg='" + this.errorMsg + '\\'' + ", ts=" + this.ts + ", data=" + this.data + '';
    
 @POST
    @Path("/test")
    public Response<Void> confirm(@Valid Param param) throws Exception 

         try 
          
            return Response.ok();

         catch (ServiceException ex) 

            log.error(ex.getMessage(), ex);

            return Response.error(ex.getErrorCode());
        

    

 

以上是关于随笔Java web 返回参数模板 Response的主要内容,如果未能解决你的问题,请参考以下文章

[Java] 模板引擎 Velocity 随笔

Java中方法的重载与覆盖(随笔01)

函数复习随笔

(13) PHP 随笔---Smarty模板引擎 缓存的高级应用 22

c复习过程随笔七

(12) PHP 随笔---Smarty模板引擎 单模板多缓存局部不缓存 20--21