springboot 自定义注解返回状态码401
时间: 2024-01-01 21:23:33 浏览: 378
在SpringBoot中自定义注解返回状态码401的方法如下:
```java
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public @interface CustomAnnotation {
}
```
在上述代码中,我们使用了`@ResponseStatus`注解来指定返回的HTTP状态码为401(UNAUTHORIZED)。然后我们将这个注解命名为`CustomAnnotation`,以便在需要的地方进行使用。
使用自定义注解的示例代码如下:
```java
@RestController
public class MyController {
@GetMapping("/test")
@CustomAnnotation
public String test() {
return "This is a test";
}
}
```
在上述代码中,我们在`test()`方法上使用了`@CustomAnnotation`注解,这样当请求`/test`接口时,如果没有权限,就会返回状态码401。
相关问题
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注解来捕获自定义异常并返回响应即可。
SPringboot 自定义404
### 如何在Spring Boot中配置自定义404错误页面
为了实现自定义的404错误页面,在`src/main/resources/templates/`目录下创建HTML文件,命名为`error.html`或更具体地针对HTTP状态码命名,比如`404.html`。当应用程序遇到相应的错误时会自动显示这些页面[^1]。
对于Thymeleaf模板引擎为例:
```html
<!-- src/main/resources/templates/404.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Page Not Found</title>
</head>
<body>
<h1>Error 404 - Page not found!</h1>
<p>The requested resource could not be found.</p>
<a href="/">Return to Home Page</a>
</body>
</html>
```
如果使用的是JSP视图,则应放置于`src/main/webapp/WEB-INF/views/error/404.jsp`路径之下。
另外一种方式是在全局异常处理类中通过`@ControllerAdvice`注解来捕获特定类型的异常并返回指定视图名称。这种方式更加灵活,允许开发者编写逻辑决定何时以及如何响应不同种类的客户端请求失败情况。
```java
// CustomErrorController.java
package com.example.springbootdemo;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class CustomErrorController implements ErrorController {
private static final String PATH = "/error";
@RequestMapping(value = PATH)
public String error(Model model) {
// 可选:向模型添加属性以便前端访问
model.addAttribute("customMessage", "This is a custom message from the server.");
return "404"; // 返回至templates下的404.html
}
@Override
public String getErrorPath() {
return PATH;
}
}
```
上述代码片段展示了两种不同的策略用于设置自定义404错误页面——静态资源映射和基于控制器的方法。前者适用于简单的场景而后者提供了更大的灵活性以满足复杂需求。
阅读全文
相关推荐















