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

时间:2019-03-18 10:14:13

标签: java android ios oracle

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);
               /* headers.add("userid",Empcode);
                headers.add("pwd",password);*/

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



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

                                        ResponseEntity<UserValidation> result= restTemplate.exchange(uri, HttpMethod.POST,entity, UserValidation.class);
                            out.print("result..."+result.getBody().getResponseMsg());       

我做了所有添加了rest api依赖项的操作, 这个女巫我仍然在application / json中响应内容;它不起作用,我只能在某些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)

当我在构建路径的项目中添加jar文件时发生错误,即rest api jar,spring框架...

显示错误,并与他们了解Rest API的所有人进行协商。

请哪个人找到解决方案并获得更正的输出

3 个答案:

答案 0 :(得分:0)

Spring无法将您的请求有效负载(LinkedMultiValueMap)解析为JSON。请确保您使用的转换器可以做到这一点。因此,您的请求不会发送到目标系统。

请阅读以下详细信息:https://www.baeldung.com/spring-httpmessageconverter-rest

答案 1 :(得分:0)

要使用application/json接受标头检索资源,您需要MappingJackson2HttpMessageConverter,它是特殊的HttpMessageConverters之一,看起来像您丢失了。

尝试将RestTemplate初始化为后立即添加以下内容:

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

答案 2 :(得分:-1)

尝试一下-

 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;
}