Spring RestTemplate

public class HttpMessageConverterExtractorimplements ResponseExtractor {

...........................

}

Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.String] and content type [text/html;charset=utf-8]

at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:110)

at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)

at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)

at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:380)

at HttpUtils.HttpUtils.sendGeneralRequest(HttpUtils.java:115)


@Override

@SuppressWarnings({"unchecked", "rawtypes", "resource"})

public T extractData(ClientHttpResponse response)throws IOException {

MessageBodyClientHttpResponseWrapper responseWrapper =new MessageBodyClientHttpResponseWrapper(response);

? if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) {

return null;

? }

MediaType contentType = getContentType(responseWrapper);

? for (HttpMessageConverter messageConverter :this.messageConverters) {

if (messageConverterinstanceof GenericHttpMessageConverter) {

GenericHttpMessageConverter genericMessageConverter =

(GenericHttpMessageConverter) messageConverter;

? ? ? ? if (genericMessageConverter.canRead(this.responseType, null, contentType)) {

if (logger.isDebugEnabled()) {

logger.debug("Reading [" +this.responseType +"] as \"" +

contentType +"\" using [" + messageConverter +"]");

? ? ? ? ? ? }

return (T) genericMessageConverter.read(this.responseType, null, responseWrapper);

? ? ? ? }

}

if (this.responseClass !=null) {

if (messageConverter.canRead(this.responseClass, contentType)) {

if (logger.isDebugEnabled()) {

logger.debug("Reading [" +this.responseClass.getName() +"] as \"" +

contentType +"\" using [" + messageConverter +"]");

? ? ? ? ? ? }

return (T) messageConverter.read((Class)this.responseClass, responseWrapper);

? ? ? ? }

}

}

throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " +

"for response type [" +this.responseType +"] and content type [" + contentType +"]");

}


protected boolean canRead(MediaType mediaType) {

if (mediaType ==null) {

return true;

? }

for (MediaType supportedMediaType : getSupportedMediaTypes()) {

if (supportedMediaType.includes(mediaType)) {

return true;

? ? ? }

}

return false;

}


protected boolean canWrite(MediaType mediaType) {

if (mediaType ==null || MediaType.ALL.equals(mediaType)) {

return true;

? }

for (MediaType supportedMediaType : getSupportedMediaTypes()) {

if (supportedMediaType.isCompatibleWith(mediaType)) {

return true;

? ? ? }

}

return false;

}


public interface HttpMessageConverter {

boolean canRead(Class var1, MediaType var2);

? ? boolean canWrite(Class var1, MediaType var2);

? ? List getSupportedMediaTypes();

? ? T read(Class var1, HttpInputMessage var2)throws IOException, HttpMessageNotReadableException;

? ? void write(T var1, MediaType var2, HttpOutputMessage var3)throws IOException, HttpMessageNotWritableException;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容