Resttemplate form / multipart:POST中的图像+ JSON

时间:2015-03-21 15:04:24

标签: java spring rest resttemplate

我正在尝试调用一个休息的ws(使用resttemplate),它接受一个图像和一些JSON。但是,我似乎无法让它运行。

相关代码如下:

    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是一些带有所有必需JSON注释的POJO(我从另一个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);

1 个答案:

答案 0 :(得分:7)

如果您想使用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);

通过提供RestTemplate s

的集合来创建HttpMessageConverter
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);