Spring 引导控制器不将路径变量识别为可选
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();
}
}
My ProfilePicture-class 包含一个类型为 byte[] 的变量“image”。我正在尝试检索此变量。
无论如何,问题是我的控制器似乎没有将我的 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 8 及更高版本和 Spring 4.1 及更高版本,您可以使用 java.util.Optional,它在 @RequestParam、@PathVariable、@RequestHeader 和 @MatrixVariable 中受支持SpringMVC
@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/
,它应该 return 您 404 Not Found 而不是您提到的以下错误:
'java.lang.String' to required type 'java.lang.Long'; nested exception
is java.lang.NumberFormatException: For input string: "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 {
}
}
或者在 @GetMapping
上定义两个资源路径,在参数上使用 Optional
:
@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}
。所以你的映射将不起作用。
除了其他答案,在我看来,最好的处理方法是将另一个映射添加到同一方法。
@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();
}
}
使用 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();
}
}
My ProfilePicture-class 包含一个类型为 byte[] 的变量“image”。我正在尝试检索此变量。
无论如何,问题是我的控制器似乎没有将我的 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 8 及更高版本和 Spring 4.1 及更高版本,您可以使用 java.util.Optional,它在 @RequestParam、@PathVariable、@RequestHeader 和 @MatrixVariable 中受支持SpringMVC
@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/
,它应该 return 您 404 Not Found 而不是您提到的以下错误:
'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "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 {
}
}
或者在 @GetMapping
上定义两个资源路径,在参数上使用 Optional
:
@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}
。所以你的映射将不起作用。
除了其他答案,在我看来,最好的处理方法是将另一个映射添加到同一方法。
@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();
}
}