Skip to content
Advertisement

Spring RestTemplate getForObject getting 404

I’m trying to make a get request to an Api Rest but I’m always getting 404, nevertheless if try copying queryUrl in a browser or postMan it works perfectly.

restTemplate.getForObject(queryUrl, entity ,Integer.class);

I’ve also tried this:

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity(httpHeaders);
log.debug("request headers: " + entity.getHeaders());
ResponseEntity response = restTemplate.exchange(queryUrl, 
HttpMethod.GET, entity, String.class);

But nothing changes. Can anyone help me?

Advertisement

Answer

You were right Barath the usage is correct. The problem was that UriComponentBuilder was including a blank space at the en of the URL.

I’ve fixed it with a trim.

String queryUrl = UriComponentsBuilder.fromUriString(HOST)
                .path(PATH)
                .buildAndExpand(param)
                .toUriString().trim();
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement