使用element-ui的select框选择多项传递到后端报错, 无法传输, 因为选择的是数组,
先说解决方法:
我后端接口用@RequestBody ContentQueryForm contentQueryForm 来接收前端传递来的字段值
@ApiOperation("内容列表分页查询")
@PostMapping("/contentList")
@ResponseBody
public Result> queryContentList(@RequestParam(value = "pageNo",required = false,defaultValue = "1")Integer pageNo,
@RequestParam(value = "pageSize",required = false,defaultValue = "15")Integer pageSize,
@RequestBody ContentQueryForm contentQueryForm){
Result> result = communityProductService.queryContentList(pageNo, pageSize, contentQueryForm);
return result;
}
ContentQueryForm接收对象如下, 用的是 private Long[] optionsLabelneirong :
@Data
public class ContentQueryForm {
/** 关键字 文章表-标题 专题表-name 图文表-`content-文字描述`视频表- content-内容描述, 后续有用户昵称*/
private String title;
/** 内容类型 文章、图文、视频、目前不包含专题*/
private Integer type;
/** 标签 1.普通标签,2.专题标签,3.合伙人标签 4.用户标签 ,-----------标签内容 */
private List label;
/** 发布日期 */
@DateTimeFormat(pattern = "yyyy-MM-dd")
private String dateStart;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private String dateEnd;
/** 推荐 推荐表-展示已推荐 product_id 关联产品聚合表*/
private String recommend;
/** 内容标签 查询用*/
private Long[] optionsLabelneirong;
调试的结果是后端接口可以接收到 optionsLabelneirong, 具体如下:
:
vue中类似于 ajax的调用方式如下, data:代表着已json形式来传送, 而后端接口也必须使用@RequestBody来接收 (而且有个坑, 那就是 data: query 这个冒号和query参数直接要有个空格, 否则可能数据传输不过去) :
在vue的data中定义的字段如下:
下面是错误的相关信息:
我的报错是基于这样方面的原因
request请求写成了params, 这是key-value的形式, 接口不能用@RequestBody, 否则会报错, 提示的大概信息是数据传送格式不对,
那么我的接口接收就变成了, 很明显这个 ContentQueryForm 是以key-value形式接收的
通过打开浏览器调试工具看到传递的数组变成了如下图:
会发每个传输的参数变成了
optionsLabelneirong[]:6
optionsLabelneirong[]:5
每一个参数后面都多了一个中括号[ ], 而接口的Form表单中的参数为 private Long[] optionsLabelneirong; 很明显这样的话, 接口是接收不到前端传递的数据, 而会报如下图一样的错误
异常信息
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[?:1.8.0_65]
at java.lang.Integer.parseInt(Integer.java:592) ~[?:1.8.0_65]
at java.lang.Integer.parseInt(Integer.java:615) ~[?:1.8.0_65]
at org.springframework.beans.AbstractNestablePropertyAccessor.processKeyedProperty(AbstractNestablePropertyAccessor.java:302) ~[spring-beans-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:287) ~[spring-beans-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:278) ~[spring-beans-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95) ~[spring-beans-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:860) ~[spring-context-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.validation.DataBinder.doBind(DataBinder.java:756) ~[spring-context-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:192) ~[spring-web-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:106) ~[spring-web-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.bindRequestParameters(ServletModelAttributeMethodProcessor.java:152) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:111) ~[spring-web-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) ~[spring-web-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) ~[spring-web-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.16.RELEASE.jar:4.3.16.RELEASE]
... 66 more
2018-10-17 15:16:42,676 [XNIO-2 task-31] ERROR [com.chehejia.aisp.boot.error.AispErrorAttributes.getErrorAttributes:52] URL:/contentproduct/contentList error status:500
2018-10-17 15:16:42,676 [XNIO-2 task-31] ERROR [com.chehejia.aisp.boot.error.ExceptionTranslator.handleError:54] 通用异常
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[?:1.8.0_65]
at java.lang.Integer.parseInt(Integer.java:592) ~[?:1.8.0_65]
at java.lang.Integer.parseInt(Integer.java:615) ~[?:1.8.0_65]
at org.springframework.beans.AbstractNestablePropertyAccessor.processKeyedProperty(AbstractNestablePropertyAccessor.java:302) ~[spring-beans-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:287) ~[spring-beans-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:278) ~[spring-beans-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95) ~[spring-beans-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:860) ~[spring-context-4.3.16.RELEASE.jar:4.3.16.RELEASE]