简体   繁体   English

org.springframework.web.client.RestClientException:无法写入请求:找不到适合请求类型的 HttpMessageConverter

[英]org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type

response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();  

String Empcode="10743";
String password="IsaiVanan";

final String uri = "https://vtop9.vit.ac.in/vtoplogin/employeeLoginPost";

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);

MultiValueMap<String, String> postParams = new LinkedMultiValueMap<String, String>();
postParams.add("userid",Empcode);
postParams.add("pwd",password);

HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<> (postParams,headers);
System.out.println("Try coming1..!");

ResponseEntity<UserValidation> result = restTemplate.exchange(uri, HttpMethod.POST,entity, UserValidation.class);

System.out.print("result..."+result.getBody().getResponseMsg());        

I did everything which i add dependency of rest api, this witch i did response content in application/json still;我做了所有我添加了 rest api 依赖的事情,这个女巫我仍然在 application/json 中做了响应内容; it not working i find that solution only in some stackoverflow verify..!它不起作用我发现该解决方案仅在某些 stackoverflow 验证中..!

public class UserValidation {

    private String responseMsg ;
    private Integer responseCode;
    public String getResponseMsg() {
        return responseMsg;
    }
    public void setResponseMsg(String responseMsg) {
        this.responseMsg = responseMsg;
    }
    public Integer getResponseCode() {
        return responseCode;
    }
    public void setResponseCode(Integer responseCode) {
        this.responseCode = responseCode;
    }

}


org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [application/json]
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:597)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:436)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:377)
    at sample.doGet(sample.java:82)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:625)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1372)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

error occurs when i add jar files in project of build path, namely rest api jar, spring frameworks...当我在构建路径的项目中添加 jar 文件时发生错误,即 rest api jar、spring 框架...

error shows and consult with everyone which they did know about rest api.错误显示并咨询他们确实了解rest api的每个人。

which someone please find the solution and get the corrected output please请哪个人找到解决方案并获得更正的输出

For retrieving a resource with application/json Accept Header, you need MappingJackson2HttpMessageConverter , which is one of the special HttpMessageConverters which looks like you're missing.要使用application/json Accept Header 检索资源,您需要MappingJackson2HttpMessageConverter ,它是一种特殊的HttpMessageConverters ,看起来您似乎缺少它。

Try adding below just after initializing RestTemplate as :在将RestTemplate初始化为:

RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(new MappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(converters);
// carry on with your code

Spring is not able to parse your request payload (LinkedMultiValueMap) to JSON. Spring 无法将您的请求负载 (LinkedMultiValueMap) 解析为 JSON。 Please make sure that you use a converter that can do this.请确保您使用可以执行此操作的转换器。 So your request is not sent to the target system.所以你的请求不会发送到目标系统。

Please read this for more information: https://www.baeldung.com/spring-httpmessageconverter-rest请阅读本文以获取更多信息: https : //www.baeldung.com/spring-httpmessageconverter-rest

Try this -尝试这个 -

 private HttpEntity<MultiValueMap<String, Object>> makeExportRequestEntity(RequestObject requestObject ) throws IOException {
    MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
    bodyMap.add("DataOne", requestObject .getDataOne());
    bodyMap.add("DataTwo", requestObject .getDataTwo());

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(bodyMap, headers);

    return requestEntity;
}

暂无
暂无

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

相关问题 RestClientException:无法写入请求:找不到适合请求类型的HttpMessageConverter - RestClientException: Could not write request: no suitable HttpMessageConverter found for request type Spring 集成 org.springframework.web.client.RestClientException:没有 HttpMessageConverter - Spring Integration org.springframework.web.client.RestClientException: No HttpMessageConverter HttpMessageConverter 异常:RestClientException:无法写入请求:找不到合适的 HttpMessageConverter - HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found 无法获取用户详细信息:class org.springframework.web.client.RestClientException,无法提取响应:没有合适的 - Could not fetch user details: class org.springframework.web.client.RestClientException, Could not extract response: no suitable org.springframework.web.client.RestClientException:无法提取响应: - org.springframework.web.client.RestClientException: Could not extract response: org.springframework.web.client.RestClientException 中的错误:java.util.HashMap 没有 HttpMessageConverter - Error in org.springframework.web.client.RestClientException: No HttpMessageConverter for java.util.HashMap org.springframework.web.client.RestClientException:提取类型和内容类型的响应时出错 [application/json;charset=utf-8] - org.springframework.web.client.RestClientException: Error while extracting response for type and content type [application/json;charset=utf-8] RestClientException无法提取响应:找不到合适的HttpMessageConverter作为响应类型 - RestClientException Could not extract response: no suitable HttpMessageConverter found for response type 无法写入请求:找不到适合请求类型和内容类型的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] RestTemplate无法编写请求:找不到适合请求类型[java.util.HashMap]的HttpMessageConverter - RestTemplate Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM