【发布时间】:2020-02-05 12:14:58
【问题描述】:
所以我在控制器中有一堆嵌套路由。如果特定的嵌套路由不存在,我想确保应用程序抛出错误。现在,如果嵌套级别大于 2,则应用程序返回 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 而不是显示错误,它会返回 200OK 并带有 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 错误吗?
【问题讨论】:
标签: java rest spring-boot http-status-code-404