国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Could not extract response: no suitable `HttpMessageConverter` found for response type [class wechat.xx] and content type [text/plain] 問題

這篇具有很好參考價(jià)值的文章主要介紹了Could not extract response: no suitable `HttpMessageConverter` found for response type [class wechat.xx] and content type [text/plain] 問題。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

1. 問題復(fù)現(xiàn)

話不多說,先貼出問題代碼:這里的GetUserInfoByAccessToken是我自定義的一個(gè)實(shí)體類。

GetUserInfoByAccessToken getUserInfoByAccessTokenString = restTemplate.getForObject(userInfoByAccessCodeURL, GetUserInfoByAccessToken.class);

異常信息:Could not extract response: no suitable HttpMessageConverter found for response type [class wechat.wxRes.GetUserInfoByAccessToken] and content type [text/plain],很明顯這段異常的意思是在指定返回類型為GetUserInfoByAccessToken,并且服務(wù)端響應(yīng)報(bào)文的content-type為text/plain的情況下找不到一個(gè)合適的HttpMessageConverter 來處理這種情況

2. 處理方法

這里舉例兩種處理請(qǐng)求

1.首先StringHttpMessageConverter這個(gè)處理器是可以處理content-type為text/plain的響應(yīng)報(bào)文的。但閱讀源碼知道必須放回類型是String才可以使用它,所有我們需要改寫下代碼,將放回類型改為String。需要的時(shí)候可以利用JSON工具類將其轉(zhuǎn)為你需要的類型。

GetUserInfoByAccessToken getUserInfoByAccessTokenString = restTemplate.getForObject(userInfoByAccessCodeURL, String.class);

需要注意的是使用StringHttpMessageConverter容易出現(xiàn)中文亂碼的情況,因?yàn)樗J(rèn)支持的字符集是ISO-8859-1,這種時(shí)候可以參考以下代碼更改StringHttpMessageConverter的默認(rèn)字符集,我這里將其改為utf-8了。

RestTemplate customRestTemplate = new RestTemplate();
List<HttpMessageConverter<?>> list = customRestTemplate.getMessageConverters();
for (HttpMessageConverter<?> httpMessageConverter : list) {
    if(httpMessageConverter instanceof StringHttpMessageConverter) {
       ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName("utf-8"));
                break;
      }
 }

2.往restTemplate的轉(zhuǎn)換器里再加一個(gè)支持JSON轉(zhuǎn)換的轉(zhuǎn)換器,比如MappingJackson2HttpMessageConverter。

RestTemplate customRestTemplate = new RestTemplate();
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_HTML,MediaType.TEXT_PLAIN));
restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
GetUserInfoByAccessToken getUserInfoByAccessTokenString = restTemplate.getForObject(userInfoByAccessCodeURL, GetUserInfoByAccessToken.class);

Could not extract response: no suitable `HttpMessageConverter` found for response type [class wechat.xx] and content type [text/plain] 問題

3. 源碼分析問題

3.1 關(guān)鍵代碼extractData方法

extractData方法將接口請(qǐng)求拿到的響應(yīng)報(bào)文拿來給HttpMessageConverter解析,這里會(huì)找到合適的解析器來解析響應(yīng)報(bào)文,解析成我們指定的返回類型的數(shù)據(jù),如果找不到或者處理出現(xiàn)異常就會(huì)拋出異常。

代碼清單1-1 org.springframework.web.client.HttpMessageConverterExtractor#extractData
// 這里的入?yún)⑹钦?qǐng)求之后的響應(yīng)體
public T extractData(ClientHttpResponse response) throws IOException {
    //創(chuàng)建一個(gè)名為responseWrapper的MessageBodyClientHttpResponseWrapper,用于包裝響應(yīng)對(duì)象response,方便操作響應(yīng)數(shù)據(jù)。
	MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response);
    // 檢查響應(yīng)是否有消息體,并且消息體不為空。如果不滿足條件,則返回null。
	if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) {
		return null;
	}
    // 獲取響應(yīng)內(nèi)容類型contentType。
	MediaType contentType = getContentType(responseWrapper);

	try {
        // 遍歷已注冊(cè)的HttpMessageConverter列表。
		for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
            // 對(duì)于實(shí)現(xiàn)了GenericHttpMessageConverter接口的轉(zhuǎn)換器,檢查是否可以讀取responseType對(duì)應(yīng)的類型,并且內(nèi)容類型匹配。
			if (messageConverter instanceof GenericHttpMessageConverter) {
				GenericHttpMessageConverter<?> genericMessageConverter =
						(GenericHttpMessageConverter<?>) messageConverter;
				if (genericMessageConverter.canRead(this.responseType, null, contentType)) {
					if (logger.isDebugEnabled()) {
						ResolvableType resolvableType = ResolvableType.forType(this.responseType);
						logger.debug("Reading to [" + resolvableType + "]");
					}
					return (T) genericMessageConverter.read(this.responseType, null, responseWrapper);
				}
			}
            // 如果沒有找到合適的GenericHttpMessageConverter,則檢查是否指定了responseClass。
			if (this.responseClass != null) {
                // 如果指定了responseClass,則檢查是否有轉(zhuǎn)換器可以讀取該類型,并且內(nèi)容類型匹配。見相關(guān)代碼`canRead`方法中的代碼清單1-2
				if (messageConverter.canRead(this.responseClass, contentType)) {
					if (logger.isDebugEnabled()) {
						String className = this.responseClass.getName();
						logger.debug("Reading to [" + className + "] as \"" + contentType + "\"");
					}
                    // 如果匹配成功,使用該轉(zhuǎn)換器讀取響應(yīng)數(shù)據(jù),并返回結(jié)果。
					return (T) messageConverter.read((Class) this.responseClass, responseWrapper);
				}
			}
		}
	}
	catch (IOException | HttpMessageNotReadableException ex) {
		throw new RestClientException("Error while extracting response for type [" +
				this.responseType + "] and content type [" + contentType + "]", ex);
	}

	throw new UnknownContentTypeException(this.responseType, contentType,
			response.getRawStatusCode(), response.getStatusText(), response.getHeaders(),
			getResponseBody(response));
}

3.2 相關(guān)代碼messageConverter.canRead(this.responseClass, contentType)方法

canRead(java.lang.Class, org.springframework.http.MediaType)方法判斷當(dāng)前的HttpMessageConverter是否可以讀取響應(yīng)報(bào)文ContentType為服務(wù)端指定的數(shù)據(jù),并且內(nèi)容和你指定的返回值類型匹配。

代碼清單1-2 org.springframework.http.converter.AbstractHttpMessageConverter#canRead(java.lang.Class, org.springframework.http.MediaType)
// 判斷`HttpMessageConverter`轉(zhuǎn)換器是否可以讀取該ContentType的數(shù)據(jù),并且內(nèi)容和你指定的返回值類型匹配
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
    // supports判斷HttpMessageConverter轉(zhuǎn)換器是否支持你指定的返回類型,參考代碼清單1-3。canRead
	return supports(clazz) && canRead(mediaType);
}

這是StringHttpMessageConverter的supports方法,可以看出他可以處理返回類型為String的數(shù)據(jù)。

代碼清單1-3 org.springframework.http.converter.StringHttpMessageConverter#supports
public boolean supports(Class<?> clazz) {
	return String.class == clazz;
}

上面代碼supports方法返回true會(huì)調(diào)用canRead(org.springframework.http.MediaType)方法,這段代碼主要就是判斷當(dāng)前的HttpMessageConverter 是否可以處理content-type為服務(wù)端指定類型的響應(yīng)報(bào)文,比如content-type為text/plain。

代碼清單1-4 org.springframework.http.converter.AbstractHttpMessageConverter#canRead(org.springframework.http.MediaType)
protected boolean canRead(@Nullable MediaType mediaType) {
	if (mediaType == null) {
		return true;
	}
	for (MediaType supportedMediaType : getSupportedMediaTypes()) {
		if (supportedMediaType.includes(mediaType)) {
			return true;
		}
	}
	return false;
}

4.關(guān)鍵點(diǎn)截圖

以下是我在調(diào)試中截取的一些圖片。

這里可以看到響應(yīng)體的contentType為text/plain,接下來就要找可以處理這種響應(yīng)類型的HttpMessageConverter。

Could not extract response: no suitable `HttpMessageConverter` found for response type [class wechat.xx] and content type [text/plain] 問題

這里可以看到已注冊(cè)的HttpMessageConverter列表里面有九個(gè)元素,并且通過他們的supportedMediaTypes屬性看到他們可以處理的contentType

Could not extract response: no suitable `HttpMessageConverter` found for response type [class wechat.xx] and content type [text/plain] 問題

首先判斷HttpMessageConverter是否可以讀取我們指定的返回類,這里我指定的是我自定義的一個(gè)返回類GetUserInfoByAccessToken.class

Could not extract response: no suitable `HttpMessageConverter` found for response type [class wechat.xx] and content type [text/plain] 問題

在這里是在判斷當(dāng)前HttpMessageConverter是否可以處理當(dāng)前響content-type為text/plain的響應(yīng)報(bào)文。

Could not extract response: no suitable `HttpMessageConverter` found for response type [class wechat.xx] and content type [text/plain] 問題文章來源地址http://www.zghlxwxcb.cn/news/detail-624474.html

到了這里,關(guān)于Could not extract response: no suitable `HttpMessageConverter` found for response type [class wechat.xx] and content type [text/plain] 問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 互聯(lián)網(wǎng)大廠技術(shù)-HTTP請(qǐng)求-Springboot整合Feign更優(yōu)雅地實(shí)現(xiàn)Http服務(wù)調(diào)用
no suitable HttpMessageConverter found for response type

    互聯(lián)網(wǎng)大廠技術(shù)-HTTP請(qǐng)求-Springboot整合Feign更優(yōu)雅地實(shí)現(xiàn)Http服務(wù)調(diào)用 no suitable HttpMessageConverter found for response type

    目錄 一、SpringBoot快速整合Feign 1.添加Pom依賴 2.啟動(dòng)類添加注解 3.引用Feign服務(wù) 二、為請(qǐng)求添加Header的3種方式 1.添加固定header 2.通過接口簽名添加header 3.動(dòng)態(tài)添加header 三、為請(qǐng)求添加超時(shí)配置 1.默認(rèn)超時(shí)時(shí)間 3.超時(shí)異常 4.全局超時(shí)配置 5.為單個(gè)服務(wù)設(shè)置超時(shí)配置 四、為請(qǐng)求配

    2024年02月11日
    瀏覽(23)
  • Could not autowire. No beans of ‘DiscoveryClient‘ type found.

    Could not autowire. No beans of ‘DiscoveryClient‘ type found.

    一、導(dǎo)錯(cuò)了包 DiscoveryClient對(duì)應(yīng)有兩個(gè)包: org.springframework.cloud.client.discovery.DiscoveryClient; com.netflix.discovery.DiscoveryClient; 目前導(dǎo)入的包是: 改成第一個(gè)包,發(fā)現(xiàn)不再報(bào)紅了。

    2024年02月11日
    瀏覽(30)
  • @Autowired報(bào)錯(cuò)Could not autowire. No beans of ‘XXX‘ type found

    @Autowired報(bào)錯(cuò)Could not autowire. No beans of ‘XXX‘ type found

    ??IDEA中使用 @Autowired 報(bào)錯(cuò) Could not autowire. No beans of \\\'XXX\\\' type found ,錯(cuò)誤大致意思為:沒有匹配到類型為XXX的bean。 ??個(gè)人覺得,注入 controller 的 service 雖然一般來說我們都是注入一個(gè)接口,但是該接口有實(shí)現(xiàn)類,并且使用 @Service 進(jìn)行關(guān)聯(lián),所以注入類型應(yīng)該也可以視為一

    2024年02月07日
    瀏覽(26)
  • idea報(bào)錯(cuò):Could not autowire. No beans of ‘UserService‘ type found.

    點(diǎn)個(gè)關(guān)注,必回關(guān) 翻譯:無法自動(dòng)連線。未找到“用戶服務(wù)”類型的服務(wù)類。 當(dāng)報(bào)錯(cuò)之后idea會(huì)提示錯(cuò)誤,不過程序的編譯和運(yùn)行都是沒有問題的(這個(gè)錯(cuò)誤提示不會(huì)產(chǎn)生任何印象) 解決方案 解決方案1: Settings - Editor - Inspections - Spring - Spring Core - Code - Autowiring for Bean Class

    2024年02月11日
    瀏覽(24)
  • idea報(bào)“Could not autowire. No beans of ‘UserMapper‘ type found. ”錯(cuò)解決辦法

    idea報(bào)“Could not autowire. No beans of ‘UserMapper‘ type found. ”錯(cuò)解決辦法

    idea具有檢測功能,接口不能直接創(chuàng)建bean的,需要用動(dòng)態(tài)代理技術(shù)來解決。 1.修改idea的配置 1.點(diǎn)擊file,選擇setting 2.搜索inspections,找到Spring 3.找到Spring子目錄下的Springcore 4.在Springcore的子目錄下找到code 5.把seyerity選項(xiàng)改成警告 2.修改代碼 1,@Autowrited改為@Autowrited(required = false)

    2024年02月05日
    瀏覽(26)
  • IDEA提示找不到Mapper接口:Could not autowire.No beans of ‘xxxMapper‘ type found

    IDEA提示找不到Mapper接口:Could not autowire.No beans of ‘xxxMapper‘ type found

    我們可以看到,上面的紅色警告在提示我們,找不到 xxxMaper 這個(gè)類型的 bean。 為啥呢? 因?yàn)?@Mapper 這個(gè)注解是 Mybatis 提供的,而 @Autowried 注解是 Spring 提供的,IDEA能理解 Spring 的上下文,但是卻和 Mybatis 關(guān)聯(lián)不上。而且我們可以根據(jù) @Autowried 源碼看到,默認(rèn)情況下,@Autowri

    2024年02月08日
    瀏覽(24)
  • 解決SpringBoot項(xiàng)目中的報(bào)錯(cuò):Could not autowire,no beans of “XXX“ type found

    解決SpringBoot項(xiàng)目中的報(bào)錯(cuò):Could not autowire,no beans of “XXX“ type found

    問題:找不到mapper注入的bean,如圖 ? 分析:注入mapper有兩種方式: ?第一種:在啟動(dòng)類中添加? @MapperScan ???????然后在mapper的類中添加? @Repository 注解 第二種方法:直接在各個(gè)mapper類中添加@Mapper注解,但是一定要注意導(dǎo)入正確的包,否則解決不了這個(gè)異常; ?很多新手

    2024年02月08日
    瀏覽(33)
  • SpringBoot - 在IDEA中經(jīng)常發(fā)現(xiàn):Could not autowire. No beans of ‘xxx‘ type found的錯(cuò)誤

    SpringBoot - 在IDEA中經(jīng)常發(fā)現(xiàn):Could not autowire. No beans of ‘xxx‘ type found的錯(cuò)誤

    錯(cuò)誤描述 在SPRINGBOOT的項(xiàng)目中,使用IDEA時(shí)經(jīng)常會(huì)遇到Could not autowire. No beans of ‘xxxx’ type found的錯(cuò)誤提示,但是程序的編譯和運(yùn)行都沒有問題,這個(gè)錯(cuò)誤提示并不影響項(xiàng)目的生產(chǎn)。 解決方案

    2024年02月15日
    瀏覽(23)
  • Qt創(chuàng)建控制臺(tái)程序選擇構(gòu)建套件問題“No suitable kits found”

    Qt創(chuàng)建控制臺(tái)程序選擇構(gòu)建套件問題“No suitable kits found”

    QT 選擇構(gòu)建套件時(shí)出現(xiàn)問題: “No suitable kits found” = 沒有找到合適的kits套件,在安裝Qt Creator時(shí)沒有安裝MinGW,所以只需要進(jìn)行安裝即可。 3.1 選擇安裝目錄下的“MaintenanceTool.exe”,雙擊計(jì)入組件安裝界面。 3.2 點(diǎn)擊“下一步” 3.3 選擇“添加或移除組件”: 3.4 根據(jù)自己安裝

    2024年02月11日
    瀏覽(21)
  • 已解決No suitable driver found for jdbc:mysql://localhost:3306/ 問題

    已解決No suitable driver found for jdbc:mysql://localhost:3306/ 問題

    已解決No suitable driver found for jdbc:mysql://localhost:3306/ 問題 在學(xué)習(xí)java數(shù)據(jù)庫連接池使用的時(shí)候遇到問題,無法連接到數(shù)據(jù)庫,查看日志是\\\"No Suitable Driver Found For Jdbc\\\",但查看數(shù)據(jù)庫連接配置沒問題。 這個(gè)問題可把我愁壞了,要不問一下GPT?上。 問了一下GPT,得到的答案是這樣的

    2024年02月07日
    瀏覽(30)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包