boot返回码规范 spring_75. Spring Boot 定制URL匹配规则【从零开始学Spring Boot】
时间: 2024-01-29 17:04:46 浏览: 23
Spring Boot 中的返回码规范主要参考了 HTTP 协议的状态码规范,以下是常见的几种状态码及其含义:
- 200 OK:表示服务器成功处理了请求。
- 201 Created:表示请求已经被成功处理,并且创建了新的资源。
- 204 No Content:表示请求已经被成功处理,但是没有返回任何内容。
- 400 Bad Request:表示客户端发送的请求有错误。
- 401 Unauthorized:表示客户端没有通过身份验证。
- 403 Forbidden:表示客户端没有权限访问该资源。
- 404 Not Found:表示请求的资源不存在。
- 500 Internal Server Error:表示服务器内部错误。
对于 Spring Boot 定制 URL 匹配规则,可以通过自定义 `WebMvcConfigurer` 来实现。具体步骤如下:
1. 创建一个类实现 `WebMvcConfigurer` 接口,并实现其 `addInterceptors` 方法。
2. 在 `addInterceptors` 方法中添加自定义的拦截器,并指定拦截的 URL 规则。
例如,以下代码实现了拦截所有以 `/api` 开头的 URL:
```java
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor())
.addPathPatterns("/api/**");
}
}
```
其中 `MyInterceptor` 是自定义的拦截器。
相关推荐
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)