我正在處理一個 Spring Boot 專案,我發現控制器類方法存在以下問題。
我有這個簡單的控制器類:
@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);
}
}
正如你所看到的,控制器類是由這個映射注釋的:
@RequestMapping("/api/admin/usertype")
現在我有了這個getUserTypeByName()控制器類方法,它應該處理像/api/admin/usertype/ADMIN這樣的 GET 請求,其中ADMIN是@PathVariable("usertype") String userType 的值
問題在于,執行與前一個類似的請求不會進入前一個控制器方法,并且 Spring Boot 回傳 404 錯誤。
為什么?我的注釋有什么問題?我錯過了什么?我該如何嘗試修復它?
uj5u.com熱心網友回復:
問題是usertype這里的路徑中沒有定義:
@GetMapping(value = "/", produces = "application/json")
您需要告訴 Spring 在哪里可以找到@PathVariable("usertype"). 所以這樣的事情會起作用
@GetMapping(value = "/{usertype}", produces = "application/json")
這是一個關于在 Spring Boot 中使用路徑變數的教程
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/398236.html
