Spring 正在尝试使用多部分文件测试rest服务
我正在尝试测试我创建的rest服务。这项服务是一种邮政服务 我想创建一个文件来传递参数,包括一个多部分文件。 从那里,我试图呼叫服务在这一点上。 很确定这项服务不起作用。但当我打电话给休息服务。我有一个简单的表单,它只传入几个值,包括jpg 这是代码Spring 正在尝试使用多部分文件测试rest服务,spring,rest,Spring,Rest,我正在尝试测试我创建的rest服务。这项服务是一种邮政服务 我想创建一个文件来传递参数,包括一个多部分文件。 从那里,我试图呼叫服务在这一点上。 很确定这项服务不起作用。但当我打电话给休息服务。我有一个简单的表单,它只传入几个值,包括jpg 这是代码 HttpMessageConverter bufferedIamageHttpMessageConverter = new ByteArrayHttpMessageConverter(); restTemplate.postForObject(
HttpMessageConverter bufferedIamageHttpMessageConverter = new ByteArrayHttpMessageConverter();
restTemplate.postForObject("http://localhost:8080/sendScreeenAsPostCard", uploadItem.getFileData(), String.class));
我的方法签名是:
ResultStatus sendScreenAsPostcard( @RequestParam MultipartFile image, @RequestParamString userId)
这就是我所犯的错误
无法写入请求:找不到适合请求类型[org.springframework.web.multipart.commons.commons multipartfile]的HttpMessageConverter
您需要模拟文件上载,这需要特定的内容类型标头、正文参数等。类似这样的操作应该可以实现以下目的:
// 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();
您需要模拟文件上载,这需要特定的内容类型标头、正文参数等。类似这样的操作应该可以实现以下目的:
// 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();
好吧,多亏了你,我想我可能会有所进步。我收到一个404错误,我认为可能与我的post方法的签名有关?看起来您使用的是带注释的Spring MVC,所以我希望您的SendScreenSpostCard方法应该有@RequestMappingmethod=post,value=/SendscreeenSpostCard之类的内容。然后,根据web.xml、容器行为等的不同,在端点之前可能会有一些内容,例如Tomcat默认情况下希望WAR名称位于主机/端口之后。你的方法中也有3个e-404是因为打字错误?最后,当Spring加载和配置正确时,您应该看到它记录了它认为它正在公开的所有端点,以便进行验证。现在我得到一个406错误。我的拼写是错误的,我改变了我的参数在我的电话这么多的想法,这就是问题所在。这是签名。我收到一个406错误,当前收到的错误是500@RequestMappingvalue=/sendScreenAsPostcard,method={RequestMethod.POST}如果我的方法只使用sendScreenAsPostcardHttpServletRequest请求,但无法获取值,我就可以让它工作。我查看了我自己使用@RequestParams的文件上载-我的每个参数都从HTTP表单中命名参数,例如:@RequestParamfile MultipartFile file、@RequestParamuserId String userId注意引号中的HTTP名称。也许可以试试。好吧,多亏了你,我想我可能会有所进步。我收到一个404错误,我认为可能与我的post方法的签名有关?看起来您使用的是带注释的Spring MVC,所以我希望您的SendScreenSpostCard方法应该有@RequestMappingmethod=post,value=/SendscreeenSpostCard之类的内容。然后,根据web.xml、容器行为等的不同,在端点之前可能会有一些内容,例如Tomcat默认情况下希望WAR名称位于主机/端口之后。你的方法中也有3个e-404是因为打字错误?最后,当Spring加载和配置正确时,您应该看到它记录了它认为它正在公开的所有端点,以便进行验证。现在我得到一个406错误。我的拼写是错误的,我改变了我的参数在我的电话这么多的想法,这就是问题所在。这是签名。我收到一个406错误,当前收到的错误是500@RequestMappingvalue=/sendScreenAsPostcard,method={RequestMethod.POST}如果我的方法只使用sendScreenAsPostcardHttpServletRequest请求,但无法获取值,我就可以让它工作。我查看了我自己使用@RequestParams的文件上载-我的每个参数都从HTTP表单中命名参数,例如:@RequestParamfile MultipartFile file、@RequestParamuserId String userId注意引号中的HTTP名称。也许你可以试试。