1

I am trying to fix an issue with my RestTemplate PUT request. Basically, the server expects data(an object) to be put in "Raw" content-type but as xml stream. I tried many combinations(of converter, content-type etc..) but nothing helps. I either end up in getting exception as " org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type com.test.myObject"

Or:

"The server encountered an error processing the request. 
The exception message is 'Incoming message for operation 'SendRequest' contains
an unrecognized http body format value 'Xml'. The expected body format value is 'Raw'. 
This can be because a WebContentTypeMapper has not been configured on the binding.
".

Any suggestions to fix this will be of great value.

4

1 回答 1

3

您可以提供自己的消息转换器:

考虑到您需要发送自定义 Content-Type,您将需要创建一个扩展类,AbstractHttpMessageConverter比如说RawHttpMessageConverter. 您需要为抽象方法提供具体的实现:

  1. supports(...)- 随时返回true
  2. readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)- 在这里您将解组您的自定义对象inputMessage.getBody() InputStream
  3. writeInternal(T t, HttpOutputMessage outputMessage)- 在这里你将把你的对象 T 编组到outputMessage.getBody() OutputStream

此外,非常重要的是设置预期内容类型的列表:new MediaType("Raw", "8");并注册到您的消息转换器列表中。

这是一种方法。另一种方法可能是扩展现有的消息转换器并仅针对您需要的内容提供具体实现。我能看到的最符合您需求的消息转换器(如果我理解正确的话)是StringHttpMessageConverter。在提供实现时,您只需创建一个 MediaTypes 列表作为类变量并向其添加“原始”类型 - 在构造函数中。覆盖getSupportedMediaTypes()并返回此列表。

设置时,RestTemplate您将拥有:

        RestTemplate restTemplate = new RestTemplate();
        List<HttpMessageConverter<AbstractHttpMessageConverter<?>>> converters = new ArrayList<HttpMessageConverter<AbstractHttpMessageConverter<?>>>();
        converters.add(new RawHttpMessageConverter());
        restTemplate.setMessageConverters(messageConverters);

为了提供更多信息,下面是我用于位图下载的自定义消息转换器:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;


public class BitmapMessageConverter implements HttpMessageConverter<Bitmap> {

    private static final int BUFFER_SIZE = 8 * 1024;
    private List<MediaType> imageMediaTypes;

    public BitmapMessageConverter() {
        imageMediaTypes = new ArrayList<MediaType>();
        imageMediaTypes.add(new MediaType("image", "*"));
        imageMediaTypes.add(new MediaType("image", "png"));
        imageMediaTypes.add(new MediaType("image", "jpeg"));
    }



    private boolean isRegisteredMediaType(MediaType mediaType) {
        return imageMediaTypes.contains(mediaType);
    }

    @Override
    public List<MediaType> getSupportedMediaTypes() {
        return imageMediaTypes;
    }

    @Override
    public Bitmap read(Class<? extends Bitmap> classArg, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        BufferedInputStream bis = new BufferedInputStream(inputMessage.getBody(), BUFFER_SIZE);
        Bitmap result = BitmapFactory.decodeStream(bis);
        return result;
    }

    @Override
    public void write(Bitmap bitmap, MediaType mediaType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        BufferedOutputStream bos = new BufferedOutputStream(outputMessage.getBody(), BUFFER_SIZE);
        bitmap.compress(CompressFormat.JPEG, 100, bos);
    }
于 2013-07-17T09:38:42.107 回答