这个 Spring 引导控制器方法注释有什么问题? (请求路径定义在class级别,路径变量定义在method)

What is wrong in this Spring Boot controller method annotation? (the path of the request is defined at class level and the path variable at method)

我正在处理 Spring 引导项目,我发现控制器 class 方法存在以下问题。

我有这个简单的控制器class:

@RestController
@RequestMapping("/api/admin/usertype")
@Log
public class UserTypeControllerAdmin {
    
    @Autowired
    UserTypeService userTypeService;
    
    @ApiOperation(
              value = "Retrieve the details of a specific user type by the type name", 
              notes = "",
              produces = "application/json")
    @GetMapping(value = "/", produces = "application/json")
    public ResponseEntity<UserType> getUSerTypeByName(@PathVariable("usertype") String userType) throws NotFoundException  {
        log.info(String.format("****** Retrieve the details of user type having name %s *******", userType));
        
        UserType retrievedUserType = userTypeService.getUserTypeByName(userType);
        
        return new ResponseEntity<UserType>(retrievedUserType, HttpStatus.OK);
            
    }

}

如您所见,控制器 class 由此映射注释:

@RequestMapping("/api/admin/usertype")

现在我有这个 getUSerTypeByName() 控制器 class 方法应该处理像 /api/admin/usertype/ADMIN 这样的 GET 请求其中 ADMIN@PathVariable("usertype") String userType

的值

问题是执行与前一个请求类似的请求不会进入前一个控制器方法,Spring 启动 return 出现 404 错误。

为什么?我的注释有什么问题?我错过了什么?我该如何修复它?

问题是 usertype 未在此处的路径中定义:

@GetMapping(value = "/", produces = "application/json")

您需要告诉 Spring 在哪里可以找到 @PathVariable("usertype")。所以像这样的东西会起作用

@GetMapping(value = "/{usertype}", produces = "application/json")

这里是 tutorial 关于在 Spring Boot

中使用路径变量