springboot自定义异常响应码为404
时间: 2023-05-22 10:02:57 浏览: 54
可以通过在异常处理类中使用@ResponseStatus注解定义响应码。如下所示:
@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException {
// ...
}
这样,当抛出NotFoundException时,就会返回404响应码。
相关问题
springboot自定义业务异常请求报错变为404
对于这个问题,您可以在Spring Boot应用程序中自定义一个异常类,然后使用@ControllerAdvice注释的类捕获并处理该异常。当您的应用程序抛出自定义异常时,您可以将它们映射到HTTP响应码404。具体来说,您可以使用@ResponseStatus注释指定您的异常类应该映射到哪个HTTP状态码。例如:
@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "业务异常")
public class MyBusinessException extends RuntimeException {
// ...
}
然后,在您的异常处理类中,您可以编写一个方法来处理此异常,并返回一个响应对象:
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(MyBusinessException.class)
public ResponseEntity<String> handleMyBusinessException(MyBusinessException ex) {
String body = "发生业务异常: " + ex.getMessage();
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
}
}
这样,当您的应用程序抛出MyBusinessException时,它将返回HTTP响应代码404,并包含一个消息,指示发生了业务异常。
springboot自定义状态码
在开发过程中,我们常常需要自定义状态码,以更好地向客户端返回请求的结果。在SpringBoot中,我们可以轻松地自定义状态码,如下所述:
首先,我们需要创建一个自定义的异常类,继承RuntimeException类,并添加一个有参的构造方法,其中包含自定义的状态码和消息。例如:
public class MyException extends RuntimeException {
private Integer code;
private String message;
public MyException(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
然后,在Controller类中,我们可以使用@ExceptionHandler注解来捕获自定义异常,并将状态码和消息作为响应返回。例如:
@RestController
public class MyController {
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
throw new MyException(1001, "自定义异常错误信息");
}
@ExceptionHandler(MyException.class)
public ResponseEntity<Result<String>> handleMyException(MyException e) {
return ResponseEntity.status(e.getCode()).body(Result.error(e.getMessage()));
}
}
在上面的例子中,我们在test()方法中抛出了自定义异常,然后在handleMyException()方法中捕获该异常,并将状态码和消息作为响应返回。
总之,在SpringBoot中自定义状态码非常简单,我们只需创建一个继承RuntimeException的异常类,并在Controller中使用@ExceptionHandler注解来捕获自定义异常并返回响应即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)