Spring-Boot : 400 Bad request error 即使存在参数
Spring-Boot : 400 Bad request error even when parameter is present
在下面的方法中有两个参数childFile
和file
,我运行这个通过邮递员调用但是当我检查它说的日志时它给了我400 Bad Request错误该参数 childFile
不存在,我附上代码、错误和邮递员截图请告诉我问题是什么
控制器
@Transactional
@RequestMapping(method = RequestMethod.POST, value = "api/addchild",produces="application/json")
public ResponseEntity<?> child(Authentication authentication,
@RequestParam(value="childFile") String childFile, @RequestParam(value="file") MultipartFile file) {
ObjectMapper mapper = new ObjectMapper();
Child child;
try {
child=mapper.readValue(childFile, Child.class);
Child createdChild=childRepo.save(child);
if(createdChild!=null)
{
childService.createChildPhoto(file, createdChild.getId());
}
return ResponseEntity.ok("child created");
} catch (IOException e1) {
e1.printStackTrace();
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Child can not be created , please check your headers and string body again.");
}
日志
Content-Disposition: form-data; name] with value ["childFile"
{
"id": 0,
"firstName": "Abeer",
"lastName": "Hashmi",
"gender": "Male",
"dateOfBirth": "1999-02-11",
"detail": null,
"emergencyNumber": "03001111115",
"medicalCondition": false,
"medicalConditionDescription": null,
"enabled": true
}
------WebKitFormBoundary3t9AJTMgmU7MV4da
Content-Disposition: form-data; name="file"; filename="child3.jpg"
Content-Type: image/jpeg
[![org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'childFile' is not present
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:198) ~\[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:109) ~\[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~\[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) \[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) \[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) \[tomcat-embed-websocket-8.5.23.jar:8.5.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) \[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE\]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) \[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE\]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) \[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE\]][1]][1]
您正在使用带有简单请求参数的表单数据,但它不是这样工作的。如果您尝试将 json 和文件一起发送,请确保 "childFile" 的内容类型是 "application/json" 并通过带有注释 @RequestPart 的请求正文接收它,并且映射生成应该是 "multipart/form-data",那么它将完成工作。
所以我找到了解决这个问题的方法。问题出在邮递员身上,当我删除 content-type header 它工作正常,邮递员自己插入 header
在下面的方法中有两个参数childFile
和file
,我运行这个通过邮递员调用但是当我检查它说的日志时它给了我400 Bad Request错误该参数 childFile
不存在,我附上代码、错误和邮递员截图请告诉我问题是什么
控制器
@Transactional
@RequestMapping(method = RequestMethod.POST, value = "api/addchild",produces="application/json")
public ResponseEntity<?> child(Authentication authentication,
@RequestParam(value="childFile") String childFile, @RequestParam(value="file") MultipartFile file) {
ObjectMapper mapper = new ObjectMapper();
Child child;
try {
child=mapper.readValue(childFile, Child.class);
Child createdChild=childRepo.save(child);
if(createdChild!=null)
{
childService.createChildPhoto(file, createdChild.getId());
}
return ResponseEntity.ok("child created");
} catch (IOException e1) {
e1.printStackTrace();
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Child can not be created , please check your headers and string body again.");
}
日志
Content-Disposition: form-data; name] with value ["childFile"
{
"id": 0,
"firstName": "Abeer",
"lastName": "Hashmi",
"gender": "Male",
"dateOfBirth": "1999-02-11",
"detail": null,
"emergencyNumber": "03001111115",
"medicalCondition": false,
"medicalConditionDescription": null,
"enabled": true
}
------WebKitFormBoundary3t9AJTMgmU7MV4da
Content-Disposition: form-data; name="file"; filename="child3.jpg"
Content-Type: image/jpeg
[![org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'childFile' is not present
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:198) ~\[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:109) ~\[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~\[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) \[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) \[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) \[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE\]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) \[tomcat-embed-websocket-8.5.23.jar:8.5.23\]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) \[tomcat-embed-core-8.5.23.jar:8.5.23\]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) \[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE\]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) \[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE\]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) \[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE\]][1]][1]
您正在使用带有简单请求参数的表单数据,但它不是这样工作的。如果您尝试将 json 和文件一起发送,请确保 "childFile" 的内容类型是 "application/json" 并通过带有注释 @RequestPart 的请求正文接收它,并且映射生成应该是 "multipart/form-data",那么它将完成工作。
所以我找到了解决这个问题的方法。问题出在邮递员身上,当我删除 content-type header 它工作正常,邮递员自己插入 header