java - HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found
问题描述
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.
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.
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);
Request (ModelReqVO) Class:
public class ModelReqVO {
private HashMap<String, String> dataMap;
ModelReqVO() {
this.dataMap = new HashMap<String, String>();
}
//getter and setter generated
}
This is the exception that I am getting-
RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.mycomp.myproj.ModelReqVO] and content type [application/json].
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?
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
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
coverter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON);
restTemplate.getMessageConverters().add(0, converter)
推荐阅读
- windows - 我应该使用哪个版本的 gcc 来正确构建 rustlearn crate?
- linux - Matlab using system-command, very slowly under linux
- python - 为什么可变内置对象不能在 Python 中进行哈希处理?这有什么好处?
- charts - 如何从详细记录上的字段创建垂直折线图
- typescript - 使用 fp-ts 重写一个用记录器包装的小函数
- python - 从 unstack() 到 Pandas 中的数据框
- c++ - 整数没有正确相加,但双打工作正常
- r - 有没有办法使用 R 根据关联来挑选数据?
- c# - 未读取数据库中的值
- javascript - Firestore 错误:“函数 Query.startAfter() 需要有效的第一个参数,但未定义。”