久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

HttpMessageConverter 異常:RestClientException:無法寫入請

HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
本文介紹了HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在編寫一個客戶端來使用 RESTful 服務.我需要以鍵值對的形式發送請求,他們建議我為此使用 Map.我調用的 RESTful 服務只會接受 JSON,而我的客戶端將使用 Java.它實際上將成為現有企業 EJB 項目的一部分.

I am writing a client to consume a RESTful service. I am required to send the request in key, value pair, they suggested that I use a Map for this. The RESTful service that I am calling is only going to accept JSON and my client is going to be in Java. It is actually going to be part of an existing enterprise EJB project.

我已經編寫了一個客戶端并且能夠成功調用 RESTful 服務.事實上,如果我以字符串(JSON 格式)發送請求,我什至會收到回復.但我想避免這種將 Map 轉換為 JSON 格式字符串然后在 Request 中發送出去的手動工作.

I have written up a client and am able to call the RESTful service successfully. In fact, if I send the request in String (JSON format) then I even get a response back. But I would like to avoid this manual work of converting the Map into a JSON format string and then sending it out in Request.

我已將 Content-Type 設置為 application/json 并創建了一個包含 KeyValue 對的 Map.

I have set Content-Type as application/json and have created a Map which will contain the KeyValue pair.

來自客戶端的代碼片段:

Snippet of code from Client:

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_JSON);
headers.add(MyConstants.JWT_AUTH_TOK, restUtil.getJWTToken());

restTemplate = new RestTemplate();

ModelReqVO modVO = new ModelReqVO();
Map<String, String> dataMap = new HashMap<String, String>();
//Setting key,value into datamap (e.g. "key1", "value1")
modVO.setDataMap(dataMap);

ResponseEntity<ModelRspnsVO> result = restTemplate.postForEntity(mySrvcFN, new HttpEntity(modVO, headers), ModelRspnsVO.class);

請求(ModelReqVO)類:

Request (ModelReqVO) Class:

public class ModelReqVO {

private HashMap<String, String> dataMap;

ModelReqVO() {
    this.dataMap = new HashMap<String, String>();
}

//getter and setter generated
}

這是我得到的例外-

RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.mycomp.myproj.ModelReqVO] and content type [application/json].

我檢查了我的 restTemplate 上的 HttpMessageConverters,我確實找到了 MappingJacksonHttpMessageConverter.代碼中還需要我使用上述轉換器嗎?

I checked the HttpMessageConverters that I have on my restTemplate and I did find MappingJacksonHttpMessageConverter. Is something else required of me in the code to use the said converter?

我在 Spring.io 論壇上找到了幾個示例,但它們是關于需要 www/form 內容而不是 JSON 的服務.令人驚訝的是,我沒有找到有關使用特定轉換器將 Map 作為 JSON 發送的任何詳細信息.

I found a couple of examples on Spring.io forums, but they were about a service which required www/form content and not JSON. I, surprisingly, do not find any details about using a particular converter for Map to be sent as a JSON.

注意:代碼片段可能有編譯錯誤,我是從手機上輸入代碼的.出于安全原因,我無法在我編碼的機器上使用互聯網.

Note: The code snippets might have compile errors, I have typed the code out from my mobile. I cannot use the internet on the machine that I code in for security reasons.

推薦答案

報錯信息說沒有找到適合請求類型的HttpMessageConverter,所以只需在MappingJackson2HttpMessageConverter>MediaType 到 RestTemplate

The error message says no suitable HttpMessageConverter found for request type, so just add MappingJackson2HttpMessageConverter with MediaType to RestTemplate

MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); 
coverter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON); 
restTemplate.getMessageConverters().add(0, converter)

這篇關于HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數據)
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創建一個HashMap.出發點)
Best way to order an HashMap by key in Java?(在 Java 中按鍵排序 HashMap 的最佳方法?)
主站蜘蛛池模板: 国产成人精品综合 | 日日骚网| 91文字幕巨乱亚洲香蕉 | 欧洲一级黄 | 久久精品国产久精国产 | 国产成人一区二区三区 | 国产精品视频免费看 | 日本免费网| 午夜视频网站 | 性色av香蕉一区二区 | 亚洲一二三区在线观看 | 国产高清不卡 | 黄色成人av | 中文字幕av一区二区三区 | 天天插天天射天天干 | 午夜影院在线观看免费 | 人人干人人爽 | av免费入口 | 91在线免费视频 | 国产有码| 欧美一a一片一级一片 | 亚洲国产aⅴ成人精品无吗 国产精品永久在线观看 | 奇色影视 | 国产精品欧美一区二区 | 在线欧美亚洲 | 在线中文字幕第一页 | 在线观看亚洲欧美 | 嫩草懂你的影院入口 | 色综合区| 亚洲高清av在线 | 99久久久久国产精品免费 | 九九伦理片 | 国产一区二区 | 日韩不卡视频在线 | 天天躁日日躁狠狠的躁天龙影院 | 精品久久久久久亚洲综合网站 | 久热m3u8| 亚洲精品一二三 | 日本三级电影免费 | 9191在线观看 | 国产婷婷 |