【发布时间】:2022-01-03 15:37:47
【问题描述】:
使用 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 类包含一个变量“image”,它的类型为 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".
有谁知道可能是什么问题?
【问题讨论】:
标签: javascript java spring-boot fetch-api