[英]Spring Boot Controller does not recognize Path Variable as optional
使用 Spring 引導,我實現了一個 RestController,如下所示:
@RestController
@RequestMapping("/api/v1/student/img")
@CrossOrigin("*")
public class ProfilePictureController {
@GetMapping( "/{studentId}")
public void getProfilePicture(@PathVariable(required = false) Long studentId, HttpServletResponse response) throws IOException {
Optional<ProfilePicture> profilePicture;
if (studentId != null) {
profilePicture= studentService.getProfilePictureByStudentId(studentId);
} else {
profilePicture= studentService.getProfilePicture(1L);
}
if (profilePicture.isPresent()) {
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(profilePicture.get().getImage());
outputStream.close();
}
}
我的 ProfilePicture 類包含一個變量“image”,它的類型為 byte[]。 我正在嘗試檢索此變量。
無論如何,問題是我的 controller 似乎沒有將我的 PathVariable 視為可選。 如果我使用 fetch-API 發送帶有以下 URL 的 GET 請求:
const url = "http://localhost:8080/api/v1/student/img/"
,
我收到一個錯誤:
'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "img"
'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "img"
。
有誰知道可能是什么問題?
你不能有可選的路徑變量,但你可以有兩個調用相同服務代碼的 controller 方法:但是
If you are using Java 8 and above and Spring 4.1 and above you can use java.util.Optional which is supported in @RequestParam, @PathVariable, @RequestHeader and @MatrixVariable in Spring MVC
@RestController
@RequestMapping("/api/v1/student/img")
@CrossOrigin("*")
public class ProfilePictureController {
@GetMapping( "/{studentId}")
public void getProfilePicture(@PathVariable Optional<Long> type studentId, HttpServletResponse response) throws IOException {
Optional<ProfilePicture> profilePicture;
if (studentId.isPresent()) {
profilePicture= studentService.getProfilePictureByStudentId(studentId.get());
} else {
profilePicture= studentService.getProfilePicture(1L);
}
if (profilePicture.isPresent()) {
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(profilePicture.get().getImage());
outputStream.close();
}
}
您只定義資源/api/v1/student/img/{studentId}
而不是資源/api/v1/student/img/
。
因此,如果您只是像您提到的那樣調用/api/v1/student/img/
,它應該返回 404 Not Found 但不是您提到的以下錯誤:
'java.lang.String' 到所需類型'java.lang.Long'; 嵌套異常是 java.lang.NumberFormatException:對於輸入字符串:“img”。
我相信您實際上是在調用/api/v1/student/img/img
。 由於img
不是 Long ,因此錯誤。
如果您只想在沒有任何 studentId 的情況下調用/api/v1/student/img/
,則應為其定義另一個資源(見下文)。 從技術上講,它們是不同的資源。
@RestController
@RequestMapping("/api/v1/student/img")
@CrossOrigin("*")
public class ProfilePictureController {
@GetMapping( "/{studentId}")
public void getProfilePicture(@PathVariable(required = false) Long studentId, HttpServletResponse response) throws IOException {
}
@GetMapping
public void getProfilePicture(HttpServletResponse response) throws IOException {
}
}
或者在參數上使用Optional
在@GetMapping
上定義兩個資源路徑:
@RestController
@RequestMapping("/api/v1/student/img")
@CrossOrigin("*")
public class ProfilePictureController {
@GetMapping( {"/", "/{studentId}"})
public void getProfilePicture(@PathVariable(required = false) Optional<Long> studentId, HttpServletResponse response) throws IOException {
}
}
/api/v1/student/img/
不匹配/api/v1/student/img/{studentId}
。 所以你的映射將不起作用。
除了其他anwser,我認為處理此問題的最佳方法是將另一個映射添加到同一方法。
@GetMapping( {"/","/{studentId}"})
public void getProfilePicture(@PathVariable(required = false) Long studentId, HttpServletResponse response) throws
IOException {
}
在此處了解更多信息https://medium.com/latesttechupdates/define-spring-optional-path-variables-1188fadfebde
嘗試這個。
在這里你可以找到一些例子==> https://www.baeldung.com/spring-pathvariable
@GetMapping( {"/","/{studentId}"})
public void getProfilePicture(@PathVariable Long studentId, HttpServletResponse response) throws
IOException {
Optional<ProfilePicture> profilePicture;
if (studentId != null) {
profilePicture= studentService.getProfilePictureByStudentId(studentId);
} else {
profilePicture= studentService.getProfilePicture(1L);
}
if (profilePicture.isPresent()) {
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(profilePicture.get().getImage());
outputStream.close();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.