1

RestTemplatePUT リクエストの問題を修正しようとしています。基本的に、サーバーは、データ (オブジェクト) が「Raw」コンテンツ タイプであるが、xml ストリームとして配置されることを期待しています。多くの組み合わせ(コンバーター、コンテンツタイプなど)を試しましたが、何も役に立ちません。私はどちらかとして例外を取得することになります" org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type com.test.myObject"

または:

"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.
".

これを修正するための提案は非常に価値があります。

4

1 に答える 1

3

独自のメッセージ コンバーターを提供できます。

カスタムの Content-Type を送信する必要があることを考えると、AbstractHttpMessageConverterlet's sayを拡張するクラスを作成する必要があります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");し、メッセージ コンバータ リストに登録することも非常に重要です。

これは 1 つの方法です。もう 1 つの方法は、既存のメッセージ コンバーターを拡張し、必要なものだけに具体的な実装を提供することです。あなたのニーズに最も近いメッセージ コンバーターは (私が正しく理解している場合)、StringHttpMessageConverterです。実装を提供するときは、MediaTypes のリストをクラス変数として作成し、それに "Raw" タイプを追加します (コンストラクター内)。このリストをオーバーライド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 に答える