0

This question is based on the solution in this thread.I implemented the top answer, but I am getting mixed results.

String stringObj = restTemplate.getForObject(http://server/rest/api/users?access_token=991949410990808314, String.class);

In code I am trying to hit a simple GET endpoint, but I am getting a 401 Unauthorized, but if I copy the rest url + token to my browser it returns the expected json object. Anyone have a clue why?

Maistrenko Vitalii
  • 994
  • 1
  • 8
  • 16
Phil Palmiero
  • 51
  • 2
  • 6

2 Answers2

0

It is not because you need to put the "Authorization" in the headers and put in you rest call? Like

HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
0

The problem I was having was with

@Autowired
RestTemplate restTemplate;

The restTemplate was not getting injected as I thought. It worked when I just did:

RestTemplate restTemplate = new RestTemplate();

If anyone has any ideas why @Autowired is not working, I'd love to hear them. Thanks.

Phil Palmiero
  • 51
  • 2
  • 6
  • It is because with Spring Boot >= 4 no default bean of RestTemplate is provided. So a bean must be explicity provided. Check this thread below: https://stackoverflow.com/questions/28024942/how-to-autowire-resttemplate-using-annotations/42618428#42618428 – srini May 11 '18 at 21:42