Spring-Boot:400即使参数存在,也会出现错误请求错误

问题描述 投票:0回答:2

在下面的方法有两个参数childFilefile,我通过postman运行这个调用,但它给我400 Bad Request错误当我检查日志它说参数childFile不存在,我附加代码,错误和postman截图请告诉我是什么问题

Postman Call

调节器

@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]
java spring spring-boot http-status-code-400 bad-request
2个回答
3
投票

所以我找到了解决这个问题的方法。问题是当邮递员删除内容类型标题时工作正常,邮递员自己插入标题


0
投票

您正在使用带有简单请求参数的表单数据,这不是它的工作原理。如果您尝试将json和文件一起发送,请确保“childFile”的内容类型为“application / json”,并通过带注释@RequestPart的请求正文接收它,并且映射生成应为“multipart / form-data”那么它会完成这项工作。

© www.soinside.com 2019 - 2024. All rights reserved.