提交表单时无法将 'java.lang.String' 类型的值转换为所需的'java.lang.Long'类型



我在控制器中有这个:

@RequestMapping(
value = "/update",
params = {"new_profile_image"},
method = RequestMethod.POST)
public ModelAndView updateUserProfileImage(
@RequestParam(value = "new_profile_image") CommonsMultipartFile newProfileImage,
ModelMap model) {
System.out.println("controller executed!");
if(newProfileImage != null && !newProfileImage.isEmpty()) {
updateService.updateUserProfileImage(newProfileImage.getBytes());
}
return new ModelAndView("redirect:/users/my_profile");
}

在 jsp 文件中:

<form action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />" method="post"  enctype="multipart/form-data">
<input name="new_profile_image" type="file" id="new_profile_image">
<button type="submit" class="btn btn-primary">Update</button>
</form>

当我提交图像时,我得到了

白标错误页 此应用程序没有显式映射/error,因此您将其视为回退。

周六 2月 08 19:08:10 CST 2020 出现意外错误(类型=错误( 请求,状态 = 400(。无法转换类型的值 'java.lang.String' 到所需的类型 'java.lang.Long';嵌套异常 是java.lang.NumberFormatException:对于输入字符串:"update">

字符串"controller executed!"永远不会显示在控制台中。我在 RequestMapping 注释中更改了value = "/update"action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />"value = "/updateImage"

更改了action="<c:url value='/users/updateImage?${_csrf.parameterName}=${_csrf.token}' />",错误消息nested exception is java.lang.NumberFormatException: For input string: "updateImage"

我不知道出了什么问题,在日食的控制台中没有显示任何异常。

编辑: 我忘了说控制器在类级别RequestMapping("/users")

现在我更改了控制器value = "/update"value = "/updateImage",但我在 jsp 页面中留下了action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />",错误仍然是:

白标错误页 此应用程序没有显式映射/error,因此您将其视为回退。

周六 2月 08 19:08:10 CST 2020 出现意外错误(类型=错误( 请求,状态 = 400(。无法转换类型的值 'java.lang.String' 到所需的类型 'java.lang.Long';嵌套异常 是java.lang.NumberFormatException:对于输入字符串:"update">

我想请求甚至没有到达控制器。

我终于通过从@RequestMapping中删除params属性来解决问题。因此,控制器最终为:

@RequestMapping(
value = "/updateImage",
method = RequestMethod.POST)
public ModelAndView updateUserProfileImage(
@RequestParam(value = "new_profile_image", required = true) CommonsMultipartFile newProfileImage,
ModelMap model) {
System.out.println("controller executed!");
if(newProfileImage != null && !newProfileImage.isEmpty()) {
updateService.updateUserProfileImage(newProfileImage.getBytes());
}
return new ModelAndView("redirect:/users/my_profile");
}

我已经在其他方法中使用了带有 post requestparams属性并且它有效,我想这里的问题与请求是多部分请求有关。但我真的不知道实际的问题是什么。

最新更新