向我的 springboot 应用程序提交数据时,必需的字符串参数 'file' 不存在 ()
Required String parameter 'file' is not present () when submitting data to my springboot app
在我的控制器中,我有这个接收数据的方法
@RequestMapping(path = "/takemyvid", method = RequestMethod.POST)
public String upload2(@RequestParam("summary") String summary, @RequestPart("file") MultipartFile file) {
System.out.println(file);
System.out.println(summary);
return "";
}
然而,当我使用正确参数的邮递员对其进行测试时,出现错误
Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'summary' is not present]
即使我只包含该文件,也会发生这种情况。
我已经在属性文件中设置了最大文件大小。
spring.servlet.multipart.max-file-size=128MB
spring.servlet.multipart.max-request-size=128MB
使用 Spring 引导版本 2.1。2.RELEASE。
我在方法参数中使用了 @ModelAttribute
注释:
@RestController
public class UploadController {
private static final Logger logger = LoggerFactory.getLogger(UploadController.class);
@RequestMapping(path = "/takemyvid", method = RequestMethod.POST)
public void upload2(@ModelAttribute FileUploadDto fileUploadDto) {
logger.info("File uploaded, summary = {}, fileSize = {} bytes", fileUploadDto.getSummary(), fileUploadDto.getFile().getSize());
}
}
而且我还像这样创建了 Dto class:
public class FileUploadDto {
private String summary;
private MultipartFile file;
//getters, setters, whatevers [...]
}
这种方式应该可行,但您应该考虑为端点更好地命名。 takemyvid
看起来不太对。
编辑
我刚刚注意到您设置了一些 header。如果它 Content-Type
设置为 Multipart,那么请删除它,因为它可能会导致关于 multipart 文件边界的异常。邮递员会自行处理。
处理必需的 VS 可选的请求参数
消息 Required String parameter 'summary' is not present
显示,您的 @RequestParam String summary
默认为 required = true
- 请参阅 java-docs。
如果你想将 API-endpoint 设计为 summary 作为 optional 然后设置 @RequestParam(required = false)
.
处理文件上传
由于您使用的是表单(键值对),我建议用 @RequestParam
注释 file 参数并让 Spring处理到指定参数类型 MultipartFile
.
的转换
Note that @RequestParam annotation can also be used to associate the part of a "multipart/form-data" request with a method argument supporting the same method argument types. The main difference is that when the method argument is not a String or raw MultipartFile / Part, @RequestParam relies on type conversion via a registered Converter or PropertyEditor while RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part. RequestParam is likely to be used with name-value form fields while RequestPart is likely to be used with parts containing more complex content e.g. JSON, XML).
另请参阅 Spring- 引导文档,版本 2.1.2,第 78.5 节 Handling Multipart File Uploads
在我的控制器中,我有这个接收数据的方法
@RequestMapping(path = "/takemyvid", method = RequestMethod.POST)
public String upload2(@RequestParam("summary") String summary, @RequestPart("file") MultipartFile file) {
System.out.println(file);
System.out.println(summary);
return "";
}
然而,当我使用正确参数的邮递员对其进行测试时,出现错误
Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'summary' is not present]
即使我只包含该文件,也会发生这种情况。
我已经在属性文件中设置了最大文件大小。
spring.servlet.multipart.max-file-size=128MB
spring.servlet.multipart.max-request-size=128MB
使用 Spring 引导版本 2.1。2.RELEASE。
我在方法参数中使用了 @ModelAttribute
注释:
@RestController
public class UploadController {
private static final Logger logger = LoggerFactory.getLogger(UploadController.class);
@RequestMapping(path = "/takemyvid", method = RequestMethod.POST)
public void upload2(@ModelAttribute FileUploadDto fileUploadDto) {
logger.info("File uploaded, summary = {}, fileSize = {} bytes", fileUploadDto.getSummary(), fileUploadDto.getFile().getSize());
}
}
而且我还像这样创建了 Dto class:
public class FileUploadDto {
private String summary;
private MultipartFile file;
//getters, setters, whatevers [...]
}
这种方式应该可行,但您应该考虑为端点更好地命名。 takemyvid
看起来不太对。
编辑
我刚刚注意到您设置了一些 header。如果它 Content-Type
设置为 Multipart,那么请删除它,因为它可能会导致关于 multipart 文件边界的异常。邮递员会自行处理。
处理必需的 VS 可选的请求参数
消息 Required String parameter 'summary' is not present
显示,您的 @RequestParam String summary
默认为 required = true
- 请参阅 java-docs。
如果你想将 API-endpoint 设计为 summary 作为 optional 然后设置 @RequestParam(required = false)
.
处理文件上传
由于您使用的是表单(键值对),我建议用 @RequestParam
注释 file 参数并让 Spring处理到指定参数类型 MultipartFile
.
Note that @RequestParam annotation can also be used to associate the part of a "multipart/form-data" request with a method argument supporting the same method argument types. The main difference is that when the method argument is not a String or raw MultipartFile / Part, @RequestParam relies on type conversion via a registered Converter or PropertyEditor while RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part. RequestParam is likely to be used with name-value form fields while RequestPart is likely to be used with parts containing more complex content e.g. JSON, XML).
另请参阅 Spring- 引导文档,版本 2.1.2,第 78.5 节 Handling Multipart File Uploads