如果路由不存在,如何配置 spring 引导以抛出 404 错误?
How to configure spring boot to throw 404 error if route does not exist?
所以我在控制器中有一堆嵌套路由。如果特定的嵌套路由不存在,我想确保应用程序抛出错误。现在,如果嵌套级别超过 2,应用程序 returns null
而不是抛出错误说找不到路由。
示例:
@RestController
@RequestMapping(value="v1/")
public class EmployeeController {
@Autowired
EmployeeRepository employeeRepository;
// Get Token Info
@RequestMapping(value = "read/tokenInfo", method = RequestMethod.GET)
public Token getTokenInfo(@AuthenticationPrincipal Token token) {
return token;
}
// List of all employees.
@GetMapping(path = "read/list")
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
在上面的代码中,假设路由 v1/read/tokenInfo
有 3 个段 - v1、read 和 tokenInfo。如果我输入错误的路线并尝试 - v1/read/tokeninfosss
而不是显示错误,它 returns 200OK with null
留言。
响应:
但是,如果路线只有 2 层 - 如下 -
@RestController
@RequestMapping(value="v1/")
public class EmployeeController {
@Autowired
EmployeeRepository employeeRepository;
// Get Token Info
@RequestMapping(value = "tokenInfo", method = RequestMethod.GET)
public Token getTokenInfo(@AuthenticationPrincipal Token token) {
return token;
}
现在如果我调用 - v1/tokenInfosss
,它会抛出一个错误 -
404 错误:
我还按如下方式配置了安全性 -
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/local/**").permitAll()
.antMatchers("/v1/read/**").hasAuthority("Display")
.antMatchers("/v1/modify/**").hasAuthority("Update")
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(getJwtAuthenticationConverter());
}
知道如何配置此 404 错误吗?
尝试以 / 结束路径并从 v1 中删除 /
@RequestMapping(value="v1")
和
@RequestMapping(value="/read/tokenInfo/", method = RequestMethod.GET)
Spring 会自动执行此操作。如果您没有正确的请求映射注释,spring 将 return 与 404。问题是在您的请求映射中。当你像那样给出 @RequestMapping(value = "read/tokenInfo", method = RequestMethod.GET)
时,它会接受任何请求,例如 read/tokenInfossesf
因为你的路径。
By default, Spring MVC performs .* suffix pattern matching so that a controller mapped to /person is also implicitly mapped to /person.*.
所以我在控制器中有一堆嵌套路由。如果特定的嵌套路由不存在,我想确保应用程序抛出错误。现在,如果嵌套级别超过 2,应用程序 returns null
而不是抛出错误说找不到路由。
示例:
@RestController
@RequestMapping(value="v1/")
public class EmployeeController {
@Autowired
EmployeeRepository employeeRepository;
// Get Token Info
@RequestMapping(value = "read/tokenInfo", method = RequestMethod.GET)
public Token getTokenInfo(@AuthenticationPrincipal Token token) {
return token;
}
// List of all employees.
@GetMapping(path = "read/list")
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
在上面的代码中,假设路由 v1/read/tokenInfo
有 3 个段 - v1、read 和 tokenInfo。如果我输入错误的路线并尝试 - v1/read/tokeninfosss
而不是显示错误,它 returns 200OK with null
留言。
响应:
但是,如果路线只有 2 层 - 如下 -
@RestController
@RequestMapping(value="v1/")
public class EmployeeController {
@Autowired
EmployeeRepository employeeRepository;
// Get Token Info
@RequestMapping(value = "tokenInfo", method = RequestMethod.GET)
public Token getTokenInfo(@AuthenticationPrincipal Token token) {
return token;
}
现在如果我调用 - v1/tokenInfosss
,它会抛出一个错误 -
404 错误:
我还按如下方式配置了安全性 -
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/local/**").permitAll()
.antMatchers("/v1/read/**").hasAuthority("Display")
.antMatchers("/v1/modify/**").hasAuthority("Update")
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(getJwtAuthenticationConverter());
}
知道如何配置此 404 错误吗?
尝试以 / 结束路径并从 v1 中删除 /
@RequestMapping(value="v1")
和
@RequestMapping(value="/read/tokenInfo/", method = RequestMethod.GET)
Spring 会自动执行此操作。如果您没有正确的请求映射注释,spring 将 return 与 404。问题是在您的请求映射中。当你像那样给出 @RequestMapping(value = "read/tokenInfo", method = RequestMethod.GET)
时,它会接受任何请求,例如 read/tokenInfossesf
因为你的路径。
By default, Spring MVC performs .* suffix pattern matching so that a controller mapped to /person is also implicitly mapped to /person.*.