Response设置状态

fetch 发送请求 -->
服务器返回 response(带有location) 并且带有状态码302 -->
浏览器接收到响应,通过location进行跳转 -->
服务器返回 response 并且带有状态码(比如200) -->

// response 响应拦截器
instance.interceptors.response.use(
response => {
return response.data
},
err => {
if (err && err.response) {
let messahe = “”;
if (err.response.data.msg) {
messahe = err.response.data.msg;
}else{
messahe = err.toString();
}
switch (err.response.status) {
case 400:
err.message = messahe
break
case 401:
err.message = ‘未授权,请登录’
break
case 403:
err.message = ‘拒绝访问’
break
case 404:
err.message = 请求地址出错: ${err.response.config.url}
break
case 408:
err.message = ‘请求超时’
break
case 500:
err.message = messahe
break
case 501:
err.message = ‘服务未实现’
break
case 502:
err.message = ‘网关错误’
break
case 503:
err.message = ‘服务不可用’
break
case 504:
err.message = ‘网关超时’
break
case 505:
err.message = ‘HTTP版本不受支持’
break
default:
}
}
return Promise.reject(err) // 返回接口返回的错误信息
}
)

package com.louis.mango.admin.common;
import com.louis.mango.admin.exception.MangoException;
import com.louis.mango.admin.exception.TokenException;
import com.louis.mango.admin.http.HttpResult;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//2开头 (请求成功)表示成功处理了请求的状态代码。
//200 (成功) 服务器已成功处理了请求。 通常,这表示服务器提供了请求的网页。
//201 (已创建) 请求成功并且服务器创建了新的资源。
//202 (已接受) 服务器已接受请求,但尚未处理。
//203 (非授权信息) 服务器已成功处理了请求,但返回的信息可能来自另一来源。
//204 (无内容) 服务器成功处理了请求,但没有返回任何内容。
//205 (重置内容) 服务器成功处理了请求,但没有返回任何内容。
//206 (部分内容) 服务器成功处理了部分 GET 请求。
//
//3开头 (请求被重定向)表示要完成请求,需要进一步操作。 通常,这些状态代码用来重定向。
//
//300 (多种选择) 针对请求,服务器可执行多种操作。 服务器可根据请求者 (user agent) 选择一项操作,或提供操作列表供请求者选择。
//301 (永久移动) 请求的网页已永久移动到新位置。 服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自动将请求者转到新位置。
//302 (临时移动) 服务器目前从不同位置的网页响应请求,但请求者应继续使用原有位置来进行以后的请求。
//303 (查看其他位置) 请求者应当对不同的位置使用单独的 GET 请求来检索响应时,服务器返回此代码。
//304 (未修改) 自从上次请求后,请求的网页未修改过。 服务器返回此响应时,不会返回网页内容。
//305 (使用代理) 请求者只能使用代理访问请求的网页。 如果服务器返回此响应,还表示请求者应使用代理。
//307 (临时重定向) 服务器目前从不同位置的网页响应请求,但请求者应继续使用原有位置来进行以后的请求。
//
//4开头 (请求错误)这些状态代码表示请求可能出错,妨碍了服务器的处理。
//
//400 (错误请求) 服务器不理解请求的语法。
//401 (未授权) 请求要求身份验证。 对于需要登录的网页,服务器可能返回此响应。
//403 (禁止) 服务器拒绝请求。
//404 (未找到) 服务器找不到请求的网页。
//405 (方法禁用) 禁用请求中指定的方法。
//406 (不接受) 无法使用请求的内容特性响应请求的网页。
//407 (需要代理授权) 此状态代码与 401(未授权)类似,但指定请求者应当授权使用代理。
//408 (请求超时) 服务器等候请求时发生超时。
//409 (冲突) 服务器在完成请求时发生冲突。 服务器必须在响应中包含有关冲突的信息。
//410 (已删除) 如果请求的资源已永久删除,服务器就会返回此响应。
//411 (需要有效长度) 服务器不接受不含有效内容长度标头字段的请求。
//412 (未满足前提条件) 服务器未满足请求者在请求中设置的其中一个前提条件。
//413 (请求实体过大) 服务器无法处理请求,因为请求实体过大,超出服务器的处理能力。
//414 (请求的 URI 过长) 请求的 URI(通常为网址)过长,服务器无法处理。
//415 (不支持的媒体类型) 请求的格式不受请求页面的支持。
//416 (请求范围不符合要求) 如果页面无法提供请求的范围,则服务器会返回此状态代码。
//417 (未满足期望值) 服务器未满足"期望"请求标头字段的要求。
//
//5开头(服务器错误)这些状态代码表示服务器在尝试处理请求时发生内部错误。 这些错误可能是服务器本身的错误,而不是请求出错。
//
//500 (服务器内部错误) 服务器遇到错误,无法完成请求。
//501 (尚未实施) 服务器不具备完成请求的功能。 例如,服务器无法识别请求方法时可能会返回此代码。
//502 (错误网关) 服务器作为网关或代理,从上游服务器收到无效响应。
//503 (服务不可用) 服务器目前无法使用(由于超载或停机维护)。 通常,这只是暂时状态。
//504 (网关超时) 服务器作为网关或代理,但是没有及时从上游服务器收到请求。
//505 (HTTP 版本不受支持) 服务器不支持请求中所用的 HTTP 协议版本。

@RestControllerAdvice
public class GlobalExceptionHandler {

/**
 * HttpStatus = {
 *         //Informational 1xx  信息
 *         '100' : 'Continue',  //继续
 *         '101' : 'Switching Protocols',  //交换协议
 *
 *         //Successful 2xx  成功
 *         '200' : 'OK',  //OK
 *         '201' : 'Created',  //创建
 *         '202' : 'Accepted',  //已接受
 *         '203' : 'Non-Authoritative Information',  //非权威信息
 *         '204' : 'No Content',  //没有内容
 *         '205' : 'Reset Content',  //重置内容
 *         '206' : 'Partial Content',  //部分内容
 *
 *         //Redirection 3xx  重定向
 *         '300' : 'Multiple Choices',  //多种选择
 *         '301' : 'Moved Permanently',  //永久移动
 *         '302' : 'Found',  //找到
 *         '303' : 'See Other',  //参见其他
 *         '304' : 'Not Modified',  //未修改
 *         '305' : 'Use Proxy',  //使用代理
 *         '306' : 'Unused',  //未使用
 *         '307' : 'Temporary Redirect',  //暂时重定向
 *
 *         //Client Error 4xx  客户端错误
 *         '400' : 'Bad Request',  //错误的请求
 *         '401' : 'Unauthorized',  //未经授权
 *         '402' : 'Payment Required',  //付费请求
 *         '403' : 'Forbidden',  //禁止
 *         '404' : 'Not Found',  //没有找到
 *         '405' : 'Method Not Allowed',  //方法不允许
 *         '406' : 'Not Acceptable',  //不可接受
 *         '407' : 'Proxy Authentication Required',  //需要代理身份验证
 *         '408' : 'Request Timeout',  //请求超时
 *         '409' : 'Conflict',  //指令冲突
 *         '410' : 'Gone',  //文档永久地离开了指定的位置
 *         '411' : 'Length Required',  //需要Content-Length头请求
 *         '412' : 'Precondition Failed',  //前提条件失败
 *         '413' : 'Request Entity Too Large',  //请求实体太大
 *         '414' : 'Request-URI Too Long',  //请求URI太长
 *         '415' : 'Unsupported Media Type',  //不支持的媒体类型
 *         '416' : 'Requested Range Not Satisfiable',  //请求的范围不可满足
 *         '417' : 'Expectation Failed',  //期望失败
 *
 *         //Server Error 5xx  服务器错误
 *         '500' : 'Internal Server Error',  //内部服务器错误
 *         '501' : 'Not Implemented',  //未实现
 *         '502' : 'Bad Gateway',  //错误的网关
 *         '503' : 'Service Unavailable',  //服务不可用
 *         '504' : 'Gateway Timeout',  //网关超时
 *         '505' : 'HTTP Version Not Supported'  //HTTP版本不支持
 * };
 * @param err
 * @return
 */

// HttpStatus.OK = 200;//OK
// HttpStatus.BAD_REQUEST = 400;//错误的请求
// HttpStatus.UNAUTHORIZED = 401;//未授权,请登录
// HttpStatus.FORBIDDEN = 403;//拒绝访问
// HttpStatus.NOT_FOUND = 404;//请求地址出错
// HttpStatus.REQUEST_TIMEOUT = 408;//请求超时
// HttpStatus.SERVICE_UNAVAILABLE =500;//内部服务器错误
@ExceptionHandler(MangoException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
HttpResult handleException(MangoException err){
return HttpResult.error(err.getMessage());
}

@ExceptionHandler(TokenException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
HttpResult handleException(TokenException err){
    ResponseEntity.noContent().build();
    ResponseEntity.ok().build();
    ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY).header(HttpHeaders.LOCATION,"FirstServlet").build();
    return HttpResult.error(401,err.getMessage());
}

}

/**

  • 异常捕捉
    */
    public class TokenException extends RuntimeException {
    protected String errorCode;
    protected int code;

    public TokenException(int code, String message) {
    super(message);
    this.code = code;
    }

    public TokenException(int code, String errorCode, String message) {
    super(message);
    this.errorCode = errorCode;
    this.code = code;
    }

    public int getCode() {
    return this.code;
    }

    public String getErrorCode() {
    return this.errorCode;
    }
    }

/**

  • 自定义异常

  • @author Louis

  • @date Jan 12, 2019
    */
    public class MangoException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    private String msg;
    private int code = 500;

    public MangoException(String msg) {
    super(msg);
    this.msg = msg;
    }

    public MangoException(String msg, Throwable e) {
    super(msg, e);
    this.msg = msg;
    }

    public MangoException(String msg, int code) {
    super(msg);
    this.msg = msg;
    this.code = code;
    }

    public MangoException(String msg, int code, Throwable e) {
    super(msg, e);
    this.msg = msg;
    this.code = code;
    }

    public String getMsg() {
    return msg;
    }

    public void setMsg(String msg) {
    this.msg = msg;
    }

    public int getCode() {
    return code;
    }

    public void setCode(int code) {
    this.code = code;
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值