0

I have an OAuth2RestTemplate to handle the communication with a REST-API and just tried to use this in combination with a Traverson, like so:

@Bean
public Traverson traverson( final OAuth2RestTemplate restTemplate ) {
   try {
      final Traverson traverson = new Traverson( new URI( this.baseUrl ), MediaType.APPLICATION_JSON );
      traverson.setRestOperations( restTemplate );
      return traverson;
   }
   catch ( final URISyntaxException e ) {
      throw new BeanCreationException( e );
   }
}

When trying to use the traverson now, the template handles the OAuth2 dance pretty well, fetching a token and all - yet the header Authorization: Bearer ... unfortunately won't be sent by the traverson.

So when I call an endpoint now, e.g. traverson.follow( "xyz" )... the result is only the login form of the REST API provider :-(

My question: Am I missing something or is my "OAuth2RestTemplate with Traverson" approach simply not supported or the traverson meant to be used in such a manner?

Any help appreciated! Currently I would say it simply isn't possible, yet most of the time in this "Spring world" there are ways to get it done - perhaps one of you knows how!

Torgeist
  • 524
  • 4
  • 13

1 Answers1

0

I think you need to set HAL message converters predefined in Traverson.

restTemplate.setMessageConverters(Traverson.getDefaultMessageConverters(mediaTypes));

In my case I've used an extension of Traverson to use the OAuth2 RestTemplate successfully.

public class ExtendedTraverson extends Traverson {

  public ExtendedTraverson(URI baseUri, RestTemplate restTemplate, List<MediaType> mediaTypes) {
    super(baseUri, mediaTypes);
    restTemplate.setMessageConverters(getDefaultMessageConverters(mediaTypes));
    setRestOperations(restTemplate);
  }

}
xvronny
  • 84
  • 5
  • Thx for your efforts, xvronny. Unfortunately we've canceled this `Traverson` endeavor for the moment, so I haven't had the opportunity to test your suggestion. I'll see to it as soon as I may and will afterwards accept your answer gladly :-) – Torgeist Nov 13 '19 at 15:47