java - 获取resttemplate请求期间出现httpclient错误

标签 java spring-boot

当我尝试执行 GET 请求时,我不断收到以下异常。对 url 的直接 curl 请求工作正常,但事实并非如此。我正在尝试从端口 8082 上的服务调用端口 8085 上的服务。

代码

ResponseEntity<List<String>> quoteResponse = restTemplate.exchange("http://localhost:8085/rest/test/{username}", HttpMethod.GET,
        null, new ParameterizedTypeReference<List<String>>() {
        },userName);

curl

curl 'http://localhost:8082/rest/test/user1' -H "Content-Type: application/json"

错误

org.springframework.web.client.HttpClientErrorException$NotFound: 404 : [<Map><timestamp>2020-06-18T23:35:31.449+00:00</timestamp><status>404</status><error>Not Found</error><message></message><path>/rest/test/user1</path></Map>]
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:113) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:184) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]

最佳答案

您的请求 header “Content-Type: application/json”在哪里

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<JSONObject> req = new HttpEntity<>(new HashMap(), httpHeaders);
ResponseEntity<List<String>> quoteResponse = restTemplate.exchange("http://localhost:8085/rest/test/{username}", HttpMethod.GET,
        req, new ParameterizedTypeReference<List<String>>() {
        },userName);

关于java - 获取resttemplate请求期间出现httpclient错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62461092/

相关文章:

java - 如何获取对应的值

java - spring boot 无法连接非主数据库

java - 使用 "server.context-parameters.*"定义的属性在 Java EE 服务器上不起作用

java - 使用 Webflux 实现 POST

java - 如何在 JavaFX 中扩展 GridPane 以覆盖 add() 会导致 NPE、场景生成器中断

java - @jsonignore 在 spring mvc 中没有按预期工作

java - 如何获取 Map<String , string > 的 string[]

java - 如何比较两个 HashMap 以查看键是否相等?

java - 在bitcoind中创建JSONRPCCLIENT对象时出现空指针异常

unit-testing - Spring Boot 1.5 在尝试测试服务时没有找到类 def FilterRegistrationBean