I'm trying to call a rest ws (using resttemplate), that accepts an image and some JSON. However, I don't seem to be able to get it running.
The relevant code is as follows:
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
ByteArrayResource bytes = new ByteArrayResource(pictureData) {
@Override
public String getFilename() {
return pictureName;
}
};
map.add("x", x);
map.add("file", bytes);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity(map, header);
String response = restTemplate.postForObject(UPLOAD_URL, requestEntity, String.class);
Where x is some POJO with all the required JSON annotations (I receive it from another web service, that part works ok).
This thing, however, tells me: HttpMessageNotWritableException: Could not write request: no suitable HttpMessageConverter found for x.
If I change ByteArrayResource to byte[] then I get a 400 Bad Request. If I change the content type to JSON, then ByteArrayResource cannot be serialized into JSON:
Caused by: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.util.LinkedMultiValueMap["file"]->java.util.LinkedList[0]->a.b.c.["inputStream"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.util.LinkedMultiValueMap["file"]->java.util.LinkedList[0]->a.b.c.["inputStream"])
I have the following converters configured:
StringHttpMessageConverter,
MappingJackson2HttpMessageConverter
FormHttpMessageConverter
Any ideas, please? Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire