Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: how to get rootUri from RestTempalte

I'm using this configuration class to initialize RestTemplate:

@Configuration
public class RestTemplateConfig {

  @Value("${endpoint-url}")
  private String endpointUrl;

  @Bean
  public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder
        .rootUri(endpointUrl)
        .messageConverters(new MappingJackson2HttpMessageConverter())
        .build();
  }
}

And in one of my service's method I use the code:

RootUriTemplateHandler handler = (RootUriTemplateHandler) restTemplate.getUriTemplateHandler();
String uri = handler.getRootUri();
restTemplate.postForLocation(uri, request);

To get this URI. Is there an easier method to get this rootUri (without casting)? Or to execute the post request directly to rootUri?

like image 980
Greg Zuber Avatar asked Aug 11 '17 09:08

Greg Zuber


People also ask

How do I call API from RestTemplate?

Consuming PUT API by using RestTemplate - exchange() method Assume this URL http://localhost:8080/products/3 returns the below response and we are going to consume this API response by using Rest Template. Autowired the Rest Template Object. Use HttpHeaders to set the Request Headers.

What is difference between getForObject and getForEntity?

For example, the method getForObject() will perform a GET and return an object. getForEntity() : executes a GET request and returns an object of ResponseEntity class that contains both the status code and the resource as an object. getForObject() : similar to getForEntity() , but returns the resource directly.

What does RestTemplate getForEntity do?

getForEntity. Retrieve an entity by doing a GET on the specified URL. The response is converted and stored in an ResponseEntity . URI Template variables are expanded using the given URI variables, if any.


3 Answers

restTemplate.getUriTemplateHandler().expand("/")
like image 76
Tama Avatar answered Nov 01 '22 05:11

Tama


You can execute the post request directly to rootUri, as long as the uri you provide to the restTemplate.postForLocation starts with "/". In that case, Spring will automatically add the baseURI provided in the restTemplate constructor.

@Configuration
public class RestTemplateConfig {

   @Bean
   public RestTemplate restTemplate(RestTemplateBuilder builder,
                                    @Value("${endpoint-url}") String endpointUrl) {
     return builder
       .rootUri(endpointUrl)
       .messageConverters(new MappingJackson2HttpMessageConverter())
       .build();
   }
}

In your service's method:

// Make a POST call to ${endpoint-url}/foobar

String uri = "/foobar"; // The leading "/" is important!
restTemplate.postForLocation(uri, request);
like image 28
Berthier Lemieux Avatar answered Nov 01 '22 07:11

Berthier Lemieux


Sounds like you are trying to use RestTemplate to pass along the value of ${endpoint-url}. That slightly awkward looking cast works but you could perhaps consider one of these alternatives:

  • Create a provider which encapsulates the endpointUrl and your restTemplate and inject this provider wherever you need either the endpointUrl or the restTemplate. For example:

    @Component
    public class RestTemplateProvider {
    
        @Value("${endpoint-url}")
        private String endpointUrl;
    
        private final RestTemplate restTemplate;
    
        @Autowired
        public RestTemplateProvider(RestTemplateBuilder restTemplateBuilder) {
            this.restTemplate = restTemplateBuilder.rootUri(endpointUrl)
                    .messageConverters(new MappingJackson2HttpMessageConverter())
                    .build();
        }
    
        public RestTemplate provide() {
            return restTemplate;
        }
    
        public String getEndpointUrl() {
            return endpointUrl;
        }
    }
    
  • Inject @Value("${endpoint-url}") private String endpointUrl; into which ever service class needs it.

like image 45
glytching Avatar answered Nov 01 '22 06:11

glytching