使用 Spring Boot,我實作了一個 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 類包含一個變數“影像”,它的型別是 byte[]。我正在嘗試檢索此變數。
無論如何,問題是我的控制器似乎沒有將我的 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".
有誰知道可能是什么問題?
uj5u.com熱心網友回復:
您只定義資源/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,因此錯誤。
如果您只想在/api/v1/student/img/沒有任何 studentId 的情況下呼叫,您應該為它定義另一個資源(見下文)。從技術上講,它們是不同的資源。
@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 {
}
}
或定義在兩個資源路徑@GetMapping與Optional上paramater:
@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 {
}
}
uj5u.com熱心網友回復:
你不能有可選的路徑變數,但你可以有兩個呼叫相同服務代碼的控制器方法: 但是
如果您使用的是 Java 8 及更高版本和 Spring 4.1 及更高版本,則可以使用 Spring MVC 中的 @RequestParam、@PathVariable、@RequestHeader 和 @MatrixVariable 支持的 java.util.Optional
@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();
}
}
uj5u.com熱心網友回復:
/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
uj5u.com熱心網友回復:
試試這個。
在這里你可以找到一些例子 ==> 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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/402545.html
標籤:
