0

I keep getting the below exception when I try to do a GET request. A direct curl request to the url works fine but this doesn't. I'm trying to call service on port 8085 from the service on port 8082.

Code

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"

Error

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]
user3310115
  • 1,372
  • 2
  • 18
  • 48
  • The port in the curl (8082) is different to the port in the code snippet (8085). Also, just FYI - ur not sending the content-type header in the java code snippet as well - but the port is "likely" to the problem since ur getting a 404. This https://stackoverflow.com/questions/32623407/add-my-custom-http-header-to-spring-resttemplate-request-extend-resttemplate?lq=1 should help u to add the header. – code_kbd Jun 19 '20 at 00:18
  • Like i said, im trying to call the service on port 8085 from the service on port 8082 – user3310115 Jun 19 '20 at 04:16

2 Answers2

0

where is your request 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);
p p
  • 1
  • 1
0

The issue seems to be with the ports 8081 worked fine. So there seems to be some connectivity issue between 8082 and 8085. Very Strange for me.

user3310115
  • 1,372
  • 2
  • 18
  • 48