尝试使用多部分文件测试rest服务 [英] Attempting to test rest service with multipart file
本文介绍了尝试使用多部分文件测试rest服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试测试我创建的休息服务.该服务是一个帖子.
I am attempting to test a rest service I created. The service is a post.
- 我想创建一个文件来传递参数(包括一个多部分文件).
- 目前我正在尝试从那里致电服务.
请确保该服务无法正常工作.但是当我打电话给休息服务时.我有一个简单的表单,只传递了几个值,包括jpg.
Pretty sure the service that doesn't work. But when I call rest Service. I have a simple form that just passes in a couple values including the jpg.
这是代码.
HttpMessageConverter bufferedIamageHttpMessageConverter = new ByteArrayHttpMessageConverter();
restTemplate.postForObject("http://localhost:8080/sendScreeenAsPostCard", uploadItem.getFileData(), String.class));
我的方法签名是:
ResultStatus sendScreenAsPostcard( @RequestParam MultipartFile image, @RequestParamString userId)
那是我得到的错误.
Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.web.multipart.commons.CommonsMultipartFile]
推荐答案
您需要模拟文件上传,这需要特定的内容类型标头,正文参数等.
You need to simulate a file upload, which requires a particular content type header, body parameters, etc. Something like this should do the trick:
// Fill out the "form"...
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<String, Object>();
parameters.add("file", new FileSystemResource("file.jpg")); // load file into parameter
parameters.add("blah", blah); // some other form field
// Set the headers...
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "multipart/form-data"); // we are sending a form
headers.set("Accept", "text/plain"); // looks like you want a string back
// Fire!
String result = restTemplate.exchange(
"http://localhost:8080/sendScreeenAsPostCard",
HttpMethod.POST,
new HttpEntity<MultiValueMap<String, Object>>(parameters, headers),
String.class
).getBody();
这篇关于尝试使用多部分文件测试rest服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文