![](/img/trans.png)
[英]How to fix 'HTTP-415' error, during POST request in REST web service using spring boot
[英]how to send MultiValueMap containing a file in a POST request to Spring rest web service?
我想使用restTemplate發送一個包含文件和另一個自定義對象的POST請求。 我已經嘗試了下面的代碼,但沒有運行,因為它缺少java.io.File
的HtttpMessageConverter
MultiValueMap是在一個請求中發送兩個對象的正確選擇嗎? 如果沒有,有什么建議嗎? 如果是,那么如何解決消息轉換器問題?
RestTemplate rest = new RestTemplate();
HttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
HttpMessageConverter stringHttpMessageConverternew = new StringHttpMessageConverter();
List<HttpMessageConverter<?>> conv = new ArrayList<HttpMessageConverter<?>> ();
conv.add(formHttpMessageConverter);
conv.add(stringHttpMessageConverternew);
rest.setMessageConverters(conv);
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
File f = new File("testFile.txt");
map.add("file", f);
NewObject a = new NewObject("123", "xxx", "xxx",
"blahblah", "5432");
map.add("newObject", a);
String result = rest.postForObject("http://localhost:8080/test/upload", map, String.class);
System.out.println(result);
控制器:
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String uploadArtefact(@RequestBody MultiValueMap<String, Object> o)
{
logger.info("tesssting: " + o);
File x = (File) o.get("file").get(0);
logger.info("File name " + x.getName());
NewObject a = (NewObject) o.get("newObject").get(0);
logger.info("New Object: " + a);
return "finished";
}
這是我在客戶端遇到的錯誤:
Exception in thread "main" org.springframework.http.converter.HttpMessageNotWritableException: Could not write request: no suitable HttpMessageConverter found for request type [java.io.File]
at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:317)
at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:275)
at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:264)
at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:204)
at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:72)
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:751)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:540)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:503)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:331)
at uk.ac.ncl.rest.Test.main(Test.java:47)
並且這兩行記錄在服務器端:
2015-02-24 16:44:14.772 WARN 38776 --- [nio-8080-exec-1] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate deserialization for type [map type; class org.springframework.util.MultiValueMap, [simple type, class java.lang.String] -> [collection type; class java.util.List, contains [simple type, class java.lang.Object]]]: com.fasterxml.jackson.databind.JsonMappingException: Can not find a deserializer for non-concrete Map type [map type; class org.springframework.util.MultiValueMap, [simple type, class java.lang.String] -> [collection type; class java.util.List, contains [simple type, class java.lang.Object]]]
2015-02-24 16:44:14.772 WARN 38776 --- [nio-8080-exec-1] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate deserialization for type [map type; class org.springframework.util.MultiValueMap, [simple type, class java.lang.Object] -> [collection type; class java.util.List, contains [simple type, class java.lang.Object]]]: com.fasterxml.jackson.databind.JsonMappingException: Can not find a deserializer for non-concrete Map type [map type; class org.springframework.util.MultiValueMap, [simple type, class java.lang.Object] -> [collection type; class java.util.List, contains [simple type, class java.lang.Object]]]
我花了很多時間在這個錯誤上!
FormHttpMessageConverter具有3個部分轉換器:
而不是放置文件本身,您應該將其放置為Resource:
File file = new File("some_path");
Resource resource = new FileSystemResource(file);
map.add("file", resource);
FileSystemResource來自包org.springframework.core.io
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.