http接口调用常用Response返回类

问题背景

使用http调用接口的时候,通常会定义返回类型,一般使用通用的类,数据放入对象中

Response响应

1 响应类

package com.yg.fastjson.response;


import lombok.Data;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

@Data
public class Response implements Serializable {

    private static final long serialVersionUID = -1222614520893986846L;


    private T data;
    /**
     * 错误码
     */
    private String code;
    /**
     * 错误信息
     */
    private String msg;

    public Response() {}

    public Response(T data, String code, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }
    


    public static  Response success(T data){
        return new Response(data, ResponseCode.SUCESS, "ok");
    }

    public static  Response success(T data, ResultCode resultCode){
        return new Response(data, resultCode.getCode(), resultCode.getMsg());
    }

    public static  Response notFound(){
        return new Response(null, ResponseCode.FAIL, "server error");
    }


//    public static  Response internalError(){
//        return new Response(null, ResultCode.CODE_98.getCode(), ResultCode.CODE_98.getMsg());
//    }

    /**
     * 系统异常,为了不影响客户,返回数据未查的
     * @param 
     * @return
     */
    public static  Response internalError(){
        return new Response(null, ResultCode.CODE_02.getCode(), ResultCode.CODE_02.getMsg());
    }

    public static  Response internalError(String msg){
        //setResponseStatus(500);
        return new Response(null, ResponseCode.FAIL, msg);
    }

    public static  Response needLoginError(String msg){
        setResponseStatus(500);
        return new Response(null, ResponseCode.NEEDLOGIN, msg);
    }

    private static void setResponseStatus(int i) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (servletRequestAttributes == null) {
            //log.warn("failed to set response, because can't get HttpServletResponse");
            return;
        }
        final HttpServletResponse response = servletRequestAttributes.getResponse();
        if (response == null) {
            //log.warn("failed to set response, because can't get HttpServletResponse");
            return;
        }
        response.setStatus(i);
    }

    @Override
    public String toString() {
        return "Response [data:" + data + ", code:" + code + ", message:" + msg + "]";
    }

}


2 响应常用码,需要什么类型的码,可以自己加入

package com.yg.fastjson.response;

public class ResponseCode {

    /**
     * 表明该请求被成功地完成,所请求的资源发送到客户端。
     */
    public static final String SUCESS = "200";

    /**
     * 系统执行错误
     */
    public static final String FAIL = "500";


    /**
     * 系统执行错误
     */
    public static final String NEEDLOGIN = "401";

}

3 系统内部代码

package com.yg.fastjson.response;

public enum ResultCode {

    //00:成功,01:入参存在非法null,02:数据未查得,98:系统异常

    CODE_00("00","成功"),
    CODE_01("01","入参存在非法null"),
    CODE_02("02","数据未查得"),
    CODE_03("03","非规定的调用时间"),
    CODE_98("98","系统异常"),
    CODE_401("401","未登录");
    private String code;
    private String msg;


    ResultCode(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}




作为程序员第 79 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha ...

image

Lyric:同样的机会

你可能感兴趣的:(http接口调用常用Response返回类)