Table of Contents
How to consume REST based web service in Spring BOOT
Introduction
Consuming REST service is very simple and less ad-hoc than SOAP service
Create a REST Template Bean
RestTemplate is spring’s central class for synchronous client side HTTP access.It enforces REST principles and simplifies communication by handlings HTTP connections leaving application code to provide URLs and extract results.
In our code, we will create a bean where we will instantiate a new RestTemplate
1 2 3 4 5 6 | @Bean public RestTemplate rest() { return new RestTemplate(); } |
Now we have rest template instance we can use the RestTemplate methods to call web service
Consuming a service by HTTP GET method
You can use Rest Template getForObject or getForEntity methods to make an HTTP GET call. Both of these operations need a URL and the ResponseObject class.
A simple example would be like below.
1 2 3 4 5 6 | @GetMapping("/availableOperations") String getAvailableOperations() { return restTemplate.getForObject(allAvailableOperations, String.class); } |
Consuming a service by HTTP POST method
Consuming a service by POST means that we will be sending some information over HTTP to the requested service and that service based on will request will process things at its end like updating a DB, recording a transaction or something similar and will give us the result back.
In Post also we can use Response Template postForObject and postForEntity Method to send a request
Using postForObject
In below example, we are using an HTTP GET @GetMapping(“/Availability”) method which can be directly called by a browser using a URL like http://localhost:8080/Availability. In this method, we will call the postForObject method and pass the endpoint URL of the service, the request object it needs and the Response type we expect.
1 2 3 4 5 6 | @GetMapping("/availability") String getURLAvailability() { return restTemplate.postForObject(url, requestObject(), String.class); } |
Using postForEntity
Method post for Entity can be used exactly as shown above, however, the return type is ResponseEntity which represents entire HTTP Response, it has HTTP response like 200,404 etc and response body.
In the below method we have to use a REST CLIENT ( like Chrome plugins Postman or AdvancedRestClient) and directly post the JSON request. @RequestBody will automatically map the JSON object to Request object, there is no need to map any elements or create a new request object.
1 2 3 4 5 6 7 8 9 | @PostMapping("/availability") public String postlogdingAvailability(@RequestBody Availability availabilityRequest) { ResponseEntity response = restTemplate.postForEntity(url, availabilityRequest, String.class); return response.getBody(); } |
Here is how the complete code looks like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | @RestController @RequestMapping("/") public class ServiceController { @Autowired RestTemplate restTemplate; @Value("${operations.restURL}") String serviceURL; //Consuming a service by GET method @GetMapping("/availableOperations") String getAvailableOperations() { return restTemplate.getForObject(serviceURL, String.class); } /** Consuming a service by postForObject method, this method is exposed as a get operation if user doesn't post a request object we will create a new request and post it to the URL/service endpoint */ @GetMapping("/availability") String getAvailability() { return restTemplate.postForObject(serviceURL, createAvailabilityRequest(), String.class); } /** Consuming a service by postForEntity method, this method is exposed as a post operation if user post a request object(JSON) it will be automatically mapped to Request parameter. */ @PostMapping("/availability") public String postAvailability(@RequestBody Availability availabilityRequest) { ResponseEntity<String> response = restTemplate.postForEntity(serviceURL, availabilityRequest, String.class); return response.getBody(); } private Object createAvailabilityRequest() { AddOnAvailability addOnAvailability = new AddOnAvailability(); addOnAvailability.setCheckInDate("09/14/2023"); addOnAvailability.setCheckOutDate("09/16/2023"); addOnAvailability.setItemCode(""); addOnAvailability.setAddOnCategory("10"); return addOnAvailability; } @Bean public RestTemplate rest() { return new RestTemplate(); } } |
Add the URL in application.yml YAML/properties file
1 2 3 4 | operations: serviceURL: https://localhost:8080/addonavailability # This is the rest service end point which needs to be consumed. |
Maven Dependencies
Following dependencies were enough for above example to work
1 2 3 4 5 6 7 8 9 10 11 12 13 | <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> |
Cool!
Do you have an example consuming an OAuth2 Authentication before POST/GET?
Not at the moment !