2
votes

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?

3
try with postman or ARC first against the endpoint and then check with the codeBarath
As I said in the previous post in Postman works perfectly that's the weird for me. Thanks for your responseAntonio682
Without much information we can't do anything .. your usage is right. Might be u can share some url just to ensure everything is right or share the working postman one sample screenshot would do.Barath

3 Answers

5
votes

This took me a bit to figure out, so I'll leave a little note here hoping one day it helps someone. I kept getting a 404 when making a get request with ResTemplate.getForEntity(URL, Type). The URL worked in Chrome, Postman, but not in my code. It ended up being because I had an unsafe character in my URL, an % to be exact, because my URL had spaces and so instead of space the URL had %20. Postman and Chrome could handle this, but not my code. Once I replaced the unsafe character with an actual space all was well.

Hope it works out for you too.

0
votes

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

When your url contains "%", then you shoud use restTemplate.getForObject(new URI(url),xx.class), it worked for me.