我正在使用Jersey client来访问Spring MVC REST控制器,以实现图像上传功能。我得到了以下异常:
com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class org.springframework.web.multipart.commons.CommonsMultipartFile, and MIME media type, multipart/form-data, was not found我发布图像的控制器方法:
@RequestMapping(value = "/file/upload", method = RequestMethod.POST)
public String fileUpload(@RequestParam("fileUpload") MultipartFile file,
Model model, HttpServletRequest request, HttpServletResponse response)
{
try
{
ClientConfig config = new DefaultClientConfig();
com.sun.jersey.api.client.Client client = com.sun.jersey.api.client.Client.create(config);
WebResource webResource = client.resource("/save-image");
ClientResponse responseMsg = webResource
.type(MediaType.MULTIPART_FORM_DATA)
.post(ClientResponse.class, file);
}
catch (Exception e)
{
logger.error("Exception in fileUpload()", e);
return "error";
}
return "success";
}我的REST控制器方法获取post数据:
@ResponseBody
@Consumes(MediaType.MULTIPART_FORM_DATA)
@RequestMapping(value = "/save-image", method = RequestMethod.POST)
public String saveImage(@FormDataParam("file") MultipartFile file, ModelMap
model)
{
//Code to save the image
}有没有解决这个异常的办法。我已经尝试了以下堆栈解决方案,但我仍然得到相同的异常。
Jersey client exception: A message body writer was not found
Sending multiple files with Jersey: MessageBodyWriter not found for multipart/form-data
发布于 2018-06-08 14:46:09
您是否添加了multipart的依赖项?
<!-- Jersey client support -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Apache Commons FileUpload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>https://stackoverflow.com/questions/50754650
复制相似问题