简体   繁体   English

在Spring 3中如何上传图像并将JSON发布到RESTful Web服务

[英]How to upload image and post JSON to RESTful web service in Spring 3

I am trying to post a JSON entity and an image to my web service. 我正在尝试将JSON实体和图像发布到我的Web服务。 However I don't manage to get it working. 但是,我无法使其正常运行。 Last error is that the request is not a multipart request. 最后一个错误是该请求不是多部分请求。

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity savePerson(@RequestPart("data") final PersonResource personResource, @RequestPart("image") MultipartFile imageFile) { ... }

CURL 卷曲

$ curl -H "Content-Type: application/json" -X POST -F 'data={"name":"Tes
t"}' -F "image=@C:\Users\LuckyLuke\Downloads\test.jpg;type=image/jpg" localhost:8080/myapp/persons

Have I misunderstood the usage of @RequestPart annotation? 我是否误解了@RequestPart批注的用法? Is the request wrong? 请求不对吗?

As far as I can see you are using @RequestMapping in a proper way. 据我所知,您正在以适当的方式使用@RequestMapping。 My guess is that you are geting error since you are enforcing application/json content type (-H "Content-Type: application/json"). 我的猜测是,由于您要强制执行application / json内容类型(-H“ Content-Type:application / json”),因此会出错。 Try to run your program without that header (-F option should take care of setting multipart/form-data request). 尝试在没有该标头的情况下运行程序(-F选项应注意设置multipart / form-data请求)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM