我正在尝试提交具有以下字段的表格。
private String type;
private long minPrice;
private long maxPrice;
当long
类型的两个字段之一为空时,要提交结果400不良请求(在非空字段的情况下正常工作)。
这是我遇到的错误:
default message [minPrice]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'long' for property 'minPrice'; nested exception is java.lang.NumberFormatException: For input string: ""
据我了解,它试图解析空字符串 type long
还是我错了?。
现在该怎么办,即使long
类型字段的值是null
,我也能够提交此表格?
(我正在使用Spring 4.0)
您应该使用类"长"而不是类型"长",因为原始类型不能具有空值。
使用class long 而不是原始类型 long
,您可以检查null
值,以便您可以处理它们并处理它们避免不良请求错误
@RequestMapping(value="/your_path", method = RequestMethod.GET)
public String myControllerFunction(
@RequestParam("minPrice") Long min,
@RequestParam("minPrice") Long max,
@RequestParam("type") String type) {
// your controller code
}
我有不同的情况,这是代码摘录:
@Consumes(MediaType.MULTIPART_FORM_DATA)
@PermitAll
@Path("uploadSiteDocument")
void uploadSiteDocument(@FormDataParam("siteId") Long siteId,
@FormDataParam("fileTypeId") Long fileTypeId,
@FormDataParam("description") String description,
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail,
@FormDataParam("file") FormDataBodyPart body,
@FormDataParam("primaryConnection") Long pcId,
@FormDataParam("internalSolution") Long isId)
throws IOException, RepositoryException;
通过Postman,此服务仅在两个最后一个参数为空的情况下才能起作用,而不是为空值,而是空的。
唯一的两个解决方案是:
- 如果这些值为null,请检查前端侧,并将它们设置为空字符串
- 或,将这两种长时间声明为字符串类型,然后在后端解析它们
解决方案2:
...@FormDataParam("primaryConnection") **String** pcId,
@FormDataParam("internalSolution") **String** isId)