@ResponseStatus注解 与 自定义异常

@ResponseStatus加在自定义运行时异常上

示例代码: 

@ResponseStatus(HttpStatus.BAD_GATEWAY)
public class BadGatewayException extends RuntimeException {

    public BadGatewayException() {
    }

    public BadGatewayException(String message) {
        super(message);
    }

    public BadGatewayException(String message, Throwable cause) {
        super(message, cause);
    }
}

(1)如果不加@ResponseStatus,在代码中直接抛出该自定义异常,会以500的HTTP状态码响应到浏览器;

(2)如果加@ResponseStatus,在代码中直接抛出该自定义异常,会以指定的HTTP状态码和指定的reson响应到浏览器;我们自定义异常的目的就是为了让它正确表述我们的思想,所以给其设置响应状态码和原因让其准确表达我们的目的。

参考资料:

https://blog.csdn.net/ITWANGBOIT/article/details/103718926

https://blog.csdn.net/qq_17586821/article/details/79822669

猜你喜欢

转载自blog.csdn.net/flyconley/article/details/117607706