gpt4 book ai didi

java - org.springframework.web.client.RestClientException : Could not write request: no suitable HttpMessageConverter found for request type

转载 作者:行者123 更新时间:2023-11-29 13:56:44 26 4
gpt4 key购买 nike

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());

我做了一切,我添加了 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 frameworks...

错误显示并咨询他们确实了解 rest api 的每个人。

请哪位 friend 找到解决方案并获得正确的输出

最佳答案

要使用 application/json Accept Header 检索资源,您需要 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

关于java - org.springframework.web.client.RestClientException : Could not write request: no suitable HttpMessageConverter found for request type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55218992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com