REST 多部分混合请求(文件 + json)与 Spring
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38207049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
REST multipart mixed request (file+json) with Spring
提问by ElArbi
I need to send a file alongside with a json to my Spring Controller. I have the following controller class:
我需要将一个文件和一个 json 一起发送到我的 Spring 控制器。我有以下控制器类:
@Controller
@RequestMapping("/perform")
public class PerformController {
...
@RequestMapping(path = "gopdf", method = RequestMethod.POST, consumes = { "multipart/mixed" })
@ResponseStatus(HttpStatus.CREATED)
public void handleFileUpload(@RequestPart("file") MultipartFile file, @RequestPart("map") String map, HttpServletResponse response) throws Exception {
...
}
}
But when I curl on my server with the following command :
但是当我使用以下命令在我的服务器上卷曲时:
curl -H "Content-Type: multipart/form-data" -F "[email protected]; type=application/json" -F "[email protected]" -X POST localhost:9000/perform/gopdf-i -v
I get 415 unsupported Media Type !
我得到 415 不受支持的媒体类型!
Any clue ?
任何线索?
回答by ElArbi
I've found the solution: I need to use @RequestParam instead of @RequestPart
我找到了解决方案:我需要使用 @RequestParam 而不是 @RequestPart
@RequestMapping(path = "gopdf", method = RequestMethod.POST, consumes = { "multipart/form-data" })
@ResponseStatus(HttpStatus.OK)
public void handleFileUpload2(@RequestParam("file") MultipartFile file, @RequestParam("map") String jsonMap,
HttpServletResponse response) throws Exceptio
回答by Bob Kuhar
The consumes thing in the other answers didn't do crap for me. The key was getting the specific multipart/* types I wanted to support onto some headers key in the RequestMapping. It was really difficult to figure out, mostly guess work and stare at the Spring source code. I'm kind-of underwhelmed with Spring's support for this, but I have managed to make it work in our Spring Boot App, but only with Tomcat?!? Something called the MultipartResolver chokes when you configure your Boot application to use Jetty...so long Jetty. But I digress...
其他答案中的消耗内容对我来说没有废话。关键是将我想要支持的特定 multipart/* 类型添加到 RequestMapping 中的某些标头键上。真的很难弄清楚,主要是猜测工作并盯着Spring源代码。我对 Spring 对此的支持有点不知所措,但我已经设法让它在我们的 Spring Boot 应用程序中工作,但只能与 Tomcat 一起使用?!?当您将启动应用程序配置为使用 Jetty 时,称为 MultipartResolver 的东西会窒息……那么长 Jetty。但我离题了...
In my Controller I set up for multipart/mixed or multipart/form-data like...
在我的控制器中,我设置了 multipart/mixed 或 multipart/form-data 像...
@RequestMapping(value = "/user/{userId}/scouting_activities", method = RequestMethod.POST,
headers = {"content-type=multipart/mixed","content-type=multipart/form-data"})
public ResponseEntity<String> POST_v1_scouting_activities(
@RequestHeader HttpHeaders headers,
@PathVariable String userId,
@RequestPart(value = "image", required = false) MultipartFile image,
@RequestPart(value = "scouting_activity", required = true) String scouting_activity_json) {
LOG.info("POST_v1_scouting_activities: headers.getContentType(): {}", headers.getContentType());
LOG.info("POST_v1_scouting_activities: userId: {}", userId);
LOG.info("POST_v1_scouting_activities: image.originalFilename: {}, image: {}",
(image!=null) ? image.getOriginalFilename() : null, image);
LOG.info("POST_v1_scouting_activities: scouting_activity_json.getType().getName(): {}, scouting_activity: {}",
scouting_activity_json.getClass().getName(), scouting_activity_json);
return new ResponseEntity<String>("POST_v1_scouting_activities\n", HttpStatus.OK);
}
That headers thing let it uniquely identify the multipart content types it was willing to take a shot at. This lets curls like...
那个标题让它唯一地标识它愿意尝试的多部分内容类型。这让卷发像...
curl -i -X POST 'http://localhost:8080/robert/v1/140218/scouting_activities' \
-H 'Content-type:multipart/mixed' \
-F 'image=@Smile_128x128.png;type=image/png' \
-F 'scouting_activity={
"field": 14006513,
"longitude": -93.2038253,
"latitude": 38.5203231,
"note": "This is the center of Dino Head.",
"scouting_date": "2017-01-19T22:56:04.836Z"
};type=application/json'
...or...
...或者...
curl -i -X POST 'http://localhost:8080/robert/v1/140218/scouting_activities' \
-H 'Content-type:multipart/form-data' \
-F 'image=@Smile_128x128.png;type=image/png' \
-F '[email protected];type=application/json'
work.
工作。
回答by Aravind
The multipart/mixed for spring webflux(2.1.0) did not work for me. Here is an alternative approach that worked
spring webflux(2.1.0) 的 multipart/mixed 对我不起作用。这是一种有效的替代方法
- Working - spring-boot-starter-web/Multipart[] - upload files where one is the payload, another is a file itself. In my case since the payload was constant, it worked.
- Not working - spring-boot-starter-webflux/Flux. The flux is empty. I tried this https://github.com/spring-projects/spring-boot/issues/13268, but it didn't work
- 工作 - spring-boot-starter-web/Multipart[] - 上传文件,其中一个是有效负载,另一个是文件本身。在我的情况下,由于有效载荷是恒定的,所以它有效。
- 不工作 - spring-boot-starter-webflux/Flux。助焊剂是空的。我试过这个https://github.com/spring-projects/spring-boot/issues/13268,但没有用
回答by Mickael
It's maybe related to your request mapping annotation. I think accept
value is missing to determine what service can accept :
它可能与您的请求映射注释有关。我认为accept
缺少确定哪些服务可以接受的价值:
Example :
例子 :
@RequestMapping(path = "gopdf", method = RequestMethod.POST, consumes = { "multipart/mixed" }, accept = MediaType.MULTIPART_FORM_DATA_VALUE)
Import :
进口 :
import org.springframework.http.MediaType;
Documentation/API :http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/MediaType.html