簡體   English   中英

這個 Spring Boot controller 方法注釋有什么問題? (請求的路徑在 class 級別定義,路徑變量在方法中定義)

[英]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 引導項目,我發現 controller class 方法存在以下問題。

我有這個簡單的 controller 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);
            
    }

}

如您所見, controller class 由此映射注釋:

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

現在我有這個getUSerTypeByName() controller class 方法應該處理像/api/admin/usertype/ADMIN這樣的 GET 請求,其中ADMIN@PathVariable("usertype") String userType

問題是執行與前一個請求類似的請求,它不會進入前一個 controller 方法和 Spring 引導返回 404 錯誤。

為什么? 我的注釋有什么問題? 我錯過了什么? 我該如何嘗試修復它?

問題是此處的usertype中未定義用戶類型:

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

您需要告訴 Spring 在哪里可以找到@PathVariable("usertype") 所以這樣的事情會起作用

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

這里有一個關於在 Spring Boot 中使用路徑變量的教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM