简体   繁体   English

HttpMessageConverter 异常:RestClientException:无法写入请求:找不到合适的 HttpMessageConverter

[英]HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found

I am writing a client to consume a RESTful service.我正在编写一个客户端来使用 RESTful 服务。 I am required to send the request in key, value pair, they suggested that I use a Map for this.我需要以键值对的形式发送请求,他们建议我为此使用 Map。 The RESTful service that I am calling is only going to accept JSON and my client is going to be in Java.我正在调用的 RESTful 服务将只接受 JSON,而我的客户端将使用 Java。 It is actually going to be part of an existing enterprise EJB project.它实际上将成为现有企业 EJB 项目的一部分。

I have written up a client and am able to call the RESTful service successfully.我已经编写了一个客户端并且能够成功调用 RESTful 服务。 In fact, if I send the request in String (JSON format) then I even get a response back.事实上,如果我以字符串(JSON 格式)发送请求,那么我什至会得到响应。 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.但我想避免这种将 Map 转换为 JSON 格式字符串然后在 Request 中发送出去的手动工作。

I have set Content-Type as application/json and have created a Map which will contain the KeyValue pair.我已将 Content-Type 设置为 application/json 并创建了一个包含 KeyValue 对的 Map。

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:请求(ModelReqVO)类:

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.我检查了我的 restTemplate 上的 HttpMessageConverters,我确实找到了 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.我在 Spring.io 论坛上找到了几个例子,但它们是关于需要 www/form 内容而不是 JSON 的服务。 I, surprisingly, do not find any details about using a particular converter for Map to be sent as a JSON.令人惊讶的是,我没有找到有关使用特定转换器将 Map 作为 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.出于安全原因,我无法在我编码的机器上使用互联网。

The error message says no suitable HttpMessageConverter found for request type, so just add MappingJackson2HttpMessageConverter with MediaType to RestTemplate该错误消息说,没有合适的HttpMessageConverter发现请求类型,所以只需添加MappingJackson2HttpMessageConverterMediaTypeRestTemplate

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RestClientException:无法写入请求:找不到适合请求类型的HttpMessageConverter - RestClientException: Could not write request: no suitable HttpMessageConverter found for request type org.springframework.web.client.RestClientException:无法写入请求:找不到适合请求类型的 HttpMessageConverter - org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type RestClientException无法提取响应:找不到合适的HttpMessageConverter作为响应类型 - RestClientException Could not extract response: no suitable HttpMessageConverter found for response type RestTemplate无法编写请求:找不到适合请求类型[java.util.HashMap]的HttpMessageConverter - RestTemplate Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] 无法写入请求:找不到适合请求类型和内容类型的HttpMessageConverter [application / x-java-serialized-object] - Could not write request: no suitable HttpMessageConverter found for request type and content type [application/x-java-serialized-object] 无法提取响应:找不到合适的HttpMessageConverter - Could not extract response: no suitable HttpMessageConverter found 没有找到适合响应类型的 HttpMessageConverter - no suitable HttpMessageConverter found for response type 解决“找不到合适的HttpMessageConverter”错误 - Solving "no suitable HttpMessageConverter found" error Android版Spring-无法提取响应:找不到适合响应类型的HttpMessageConverter - Spring for Android - Could not extract response: no suitable HttpMessageConverter found for response type 尝试执行restclient请求时找不到合适的HttpMessageConverter - No suitable HttpMessageConverter found when trying to execute restclient request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM