Spring Boot控制器不将Path Variable识别为可选



使用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();
}
}

My ProfilePicture类包含一个变量";图像";,其类型为byte[]。我正在尝试检索此变量。

无论如何,问题是我的控制器似乎没有将我的PathVariable视为可选的。如果我使用fetch-API发送带有以下URL的GET请求:

const url = "http://localhost:8080/api/v1/student/img/"

我得到一个错误:

CCD_ 2。

有人知道可能是什么问题吗?

您只定义了资源/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

不能有可选的路径变量,但可以有两个调用相同服务代码的控制器方法:

如果您使用的是Java 8及以上版本和Spring 4.1及以上版本,则可以使用Java.util.Optial,它在Spring MVC 中的@RequestParam、@PathVariable、@RequestHeader和@MatrixVariable中受支持

@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();
}
}

试试这个。

在这里,您可以找到一些示例===>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();
}
}

最新更新