我想发送一个POST请求,其中包含一个文件和另一个使用restTemplate的自定义对象。我已经尝试过以下代码,但由于HtttpMessageConverter
java.io.File
,因此无法正常运行
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]]]
答案 0 :(得分:1)
我花了这么多时间来处理这个错误!
FormHttpMessageConverter有3个部件转换器:
不应该放置文件本身,而应将其作为资源:
File file = new File("some_path");
Resource resource = new FileSystemResource(file);
map.add("file", resource);
FileSystemResource来自包org.springframework.core.io