当我尝试执行 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/