如何将文件与包含有关文件的附加信息的对象的 json 一起上传?
Posted
技术标签:
【中文标题】如何将文件与包含有关文件的附加信息的对象的 json 一起上传?【英文标题】:How to upload a file together with a json of an object containing additional information about the file? 【发布时间】:2016-08-26 10:34:40 【问题描述】:请问如何上传 CSV 文件(产品目录)以及包含目录名称和日期的 JSON 对象?我将 Spring Boot (1.3.3.RELEASE) 与 jackson-databind 用于 JSON REST API 和 SwaggerUI (2.1.4 - 直接包含在项目中,而不是作为 Maven 依赖项)。我创建了以下端点:
@RequestMapping(method = POST, consumes = MULTIPART_FORM_DATA_VALUE)
@ApiOperation(value = "Create catalogue by uploading a file", consumes = MULTIPART_FORM_DATA_VALUE)
public Catalogue createCatalogue(@RequestPart("catalogue") Catalogue catalogue, MultipartFile file) ...
DTO 目录如下所示:
@Data
public class Catalogue
private String name;
private LocalDate validFrom;
private LocalDate validTo;
我使用 SwaggerUI 将文件和 json (textarea) 发布到此端点:
"name":"Katalog 2016"
但是当 Spring 尝试反序列化 Catalog 时出现错误,因为 contentType 为空:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported
这是日志过滤器记录的完整请求:
REQUEST: "method":"POST","path":"/api/v1/catalogues","params":"catalogue":"\"name\":\"Katalog 2016\"","headers":"content-length":"318","referer":"http://localhost:8000/swagger/index.html","accept-language":"cs","cookie":"oracle.uix=0^^GMT+1:00^p; wp-settings-time-1=1458506367; pll_language=en; 9d4bb4a09f511681369671a08beff228=9kp8k4p5rtbsvb0dtg0c585l50; 462bddfc0804b9e2d3bedb862fe829b3=jg9rhnasq47dnluai04pp9dpg0; 6b983e6bbfb12fe47cd501a09aa63a2f=t4bg8eeakngjag7pkd3qldjki1; wp-settings-2=editor%3Dhtml%26libraryContent%3Dbrowse; wp-settings-time-2=1459108118; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_86a9106ae65537651a8e456835b316ab=admin%7C1459353525%7ChXEWYSjH45ohapLSXw6NZOyIdQ0qrDGpSDieVRbNCh1%7Cd39d1b99843b348cca8c992a3908ae6aef303e9e7196d38fa6173fd91146b260; JSESSIONID=93D946F7EC2F8E8FFC880A1D5752BBEA","origin":"http://localhost:8000","host":"localhost:8000","x-requested-with":"XMLHttpRequest","connection":"keep-alive","content-type":"multipart/form-data; boundary=----WebKitFormBoundaryMnZeiNwIgldJlGGd","accept-encoding":"gzip, deflate","accept":"application/json;charset=UTF-8","user-agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36","body":[EMPTY]
请问我错过了什么?提前谢谢了。沃杰科技
更新: 我介绍了 CommonsMultipartResolver:
@Bean
public CommonsMultipartResolver multipartResolver()
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setMaxUploadSize(5242880L);
resolver.setDefaultEncoding("UTF-8");
return resolver;
我现在又遇到一个错误:
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'catalogue' is not present
【问题讨论】:
【参考方案1】:您在目录部分的端点中有错误。
注释@RequestPart
用于文件而不是对象。
可能你需要这个端点定义:
public Catalogue createCatalogue(@RequestParam("catalogue") String catalogue, @RequestPart("file") MultipartFile file)
【讨论】:
以上是关于如何将文件与包含有关文件的附加信息的对象的 json 一起上传?的主要内容,如果未能解决你的问题,请参考以下文章
IntelliSense:“没有可用的附加信息”,[请参见“C++项目 IntelliSense 疑难解答”,获得进一步的帮助]