Resttemplate form/multipart: 图片 + JSON in POST
Resttemplate form/multipart: image + JSON in POST
我正在尝试调用接受图像和一些 JSON 的 rest ws(使用 resttemplate)。不过我好像拿不到运行.
相关代码如下:
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);
其中 x 是一些 POJO,带有所有必需的 JSON 注释(我从另一个 Web 服务收到它,那部分工作正常)。
但是,这件事告诉我:HttpMessageNotWritableException:无法写入请求:找不到适合 x 的 HttpMessageConverter。
如果我将 ByteArrayResource 更改为 byte[],则会收到 400 Bad Request。
如果我将内容类型更改为 JSON,则 ByteArrayResource 无法序列化为 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"])
我配置了以下转换器:
StringHttpMessageConverter,
MappingJackson2HttpMessageConverter
FormHttpMessageConverter
有什么想法吗?
提前致谢。
更新
所以这就是我目前按照说明得到的:
我这样注册转换器:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
formHttpMessageConverter.addPartConverter(new MappingJackson2HttpMessageConverter());
formHttpMessageConverter.addPartConverter(new ResourceHttpMessageConverter()); // This is hope driven programming
restTemplate.getMessageConverters().add(new ResourceHttpMessageConverter());
restTemplate.getMessageConverters().add(formHttpMessageConverter);
然后在 ws 调用中我有:
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON); //Also tried with multipart...
MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
ByteArrayResource bytes = new ByteArrayResource(pictureData) {
@Override
public String getFilename() {
return pictureName;
}
};
HttpHeaders xHeader = new HttpHeaders();
xHeader.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<X> xPart = new HttpEntity<>(x, xHeader);
multipartRequest.add("x", xPart);
HttpHeaders pictureHeader = new HttpHeaders();
pictureHeader.setContentType(MediaType.IMAGE_PNG);
HttpEntity<ByteArrayResource> picturePart = new HttpEntity<>(bytes, pictureHeader);
multipartRequest.add("file", picturePart);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity(multipartRequest, header);
return restTemplate.postForObject(UPLOAD_URL, requestEntity, String.class);
如果要使用ByteArrayResource
,只需注册一个ResourceHttpMessageConverter
。
如果您想使用byte[]
,只需注册一个ByteArrayHttpMessageConverter
。
图像部分的内容类型应该是图像类型,如image/png
,而不是application/json
。
您可以使用
设置每个单独部分的数据类型
HttpHeaders partHeaders = new HttpHeaders();
partHeaders.setContentType(MediaType.IMAGE_PNG);
HttpEntity<ByteArrayResource> bytesPart = new HttpEntity<ByteArrayResource>(bytes, partHeaders);
map.add("file", bytesPart);
通过提供您收集的 HttpMessageConverter
创建您的 RestTemplate
HttpMessageConverter<Object> jackson = new MappingJackson2HttpMessageConverter();
HttpMessageConverter<Resource> resource = new ResourceHttpMessageConverter();
FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
formHttpMessageConverter.addPartConverter(jackson);
formHttpMessageConverter.addPartConverter(resource); // This is hope driven programming
RestTemplate restTemplate = new RestTemplate(Arrays.asList(jackson, resource, formHttpMessageConverter));
你最外层的 HttpEntity
应该有一个多部分内容类型
header.setContentType(MediaType.MULTIPART_FORM_DATA);
我正在尝试调用接受图像和一些 JSON 的 rest ws(使用 resttemplate)。不过我好像拿不到运行.
相关代码如下:
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);
其中 x 是一些 POJO,带有所有必需的 JSON 注释(我从另一个 Web 服务收到它,那部分工作正常)。
但是,这件事告诉我:HttpMessageNotWritableException:无法写入请求:找不到适合 x 的 HttpMessageConverter。
如果我将 ByteArrayResource 更改为 byte[],则会收到 400 Bad Request。 如果我将内容类型更改为 JSON,则 ByteArrayResource 无法序列化为 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"])
我配置了以下转换器:
StringHttpMessageConverter,
MappingJackson2HttpMessageConverter
FormHttpMessageConverter
有什么想法吗? 提前致谢。
更新
所以这就是我目前按照说明得到的: 我这样注册转换器:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
formHttpMessageConverter.addPartConverter(new MappingJackson2HttpMessageConverter());
formHttpMessageConverter.addPartConverter(new ResourceHttpMessageConverter()); // This is hope driven programming
restTemplate.getMessageConverters().add(new ResourceHttpMessageConverter());
restTemplate.getMessageConverters().add(formHttpMessageConverter);
然后在 ws 调用中我有:
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON); //Also tried with multipart...
MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
ByteArrayResource bytes = new ByteArrayResource(pictureData) {
@Override
public String getFilename() {
return pictureName;
}
};
HttpHeaders xHeader = new HttpHeaders();
xHeader.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<X> xPart = new HttpEntity<>(x, xHeader);
multipartRequest.add("x", xPart);
HttpHeaders pictureHeader = new HttpHeaders();
pictureHeader.setContentType(MediaType.IMAGE_PNG);
HttpEntity<ByteArrayResource> picturePart = new HttpEntity<>(bytes, pictureHeader);
multipartRequest.add("file", picturePart);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity(multipartRequest, header);
return restTemplate.postForObject(UPLOAD_URL, requestEntity, String.class);
如果要使用ByteArrayResource
,只需注册一个ResourceHttpMessageConverter
。
如果您想使用byte[]
,只需注册一个ByteArrayHttpMessageConverter
。
图像部分的内容类型应该是图像类型,如image/png
,而不是application/json
。
您可以使用
设置每个单独部分的数据类型HttpHeaders partHeaders = new HttpHeaders();
partHeaders.setContentType(MediaType.IMAGE_PNG);
HttpEntity<ByteArrayResource> bytesPart = new HttpEntity<ByteArrayResource>(bytes, partHeaders);
map.add("file", bytesPart);
通过提供您收集的 HttpMessageConverter
RestTemplate
HttpMessageConverter<Object> jackson = new MappingJackson2HttpMessageConverter();
HttpMessageConverter<Resource> resource = new ResourceHttpMessageConverter();
FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
formHttpMessageConverter.addPartConverter(jackson);
formHttpMessageConverter.addPartConverter(resource); // This is hope driven programming
RestTemplate restTemplate = new RestTemplate(Arrays.asList(jackson, resource, formHttpMessageConverter));
你最外层的 HttpEntity
应该有一个多部分内容类型
header.setContentType(MediaType.MULTIPART_FORM_DATA);