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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容