[wp_ad_camp_1]
This post demonstrates how to use Spring’s RestTemplate to consume RESTful web services in Java. The codes used are used in a SOA or Microservices context but the key code snippet is about RestTemplate.
3 Maven Projects
There are 3 Maven projects used for this post:
- common-dto-api
- This contains all Data Transfer Objects (DTOs) used, in this case, to exchange data between a config server and a client service.
- com-turreta-soademo-configserver
- This represents the config server.
- For this demo, it is configured to run on port 8080.
- com-turreta-soademo-clientservice
- This represents the client service that consumes (RESTful) services available on the config server
- For this demo, it is configured to run on port 8081.
- It has a single @RestController which you can test if the two applications work nicely with each other. See Demo Test section below.
Remote Service to Test RestTemplate
Our config server provides a RESTful web service for our client to consume using RestTemplate.
[wp_ad_camp_2]
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 | package com.turreta.soademo.configserver; import com.turreta.soademo.common.dto.AppConfigDTO; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.List; @RestController public class ConfigController { private List<AppConfigDTO> predefinedConfigList = new ArrayList<>(); @PostConstruct public void postContruct() { AppConfigDTO dto1 = new AppConfigDTO(); dto1.setApplicationName("app-1"); dto1.setVersion("1.2.3"); dto1.setIsEnabled(false); AppConfigDTO dto2 = new AppConfigDTO(); dto2.setApplicationName("app-2"); dto2.setVersion("1.2.3"); dto2.setIsEnabled(true); predefinedConfigList.add(dto1); predefinedConfigList.add(dto2); } @RequestMapping(value = "/get-config-by-name", method = RequestMethod.POST) public AppConfigDTO findConfig(@RequestBody AppConfigDTO findDTO) { AppConfigDTO rtnDto = null; for(AppConfigDTO d: this.predefinedConfigList) { if(d.getApplicationName().equals(findDTO.getApplicationName())) { rtnDto = d; break; } } return rtnDto; } } |
Our Client Service
Our client service has one @RestController. The important lines are lines 24-27 which enable us to perform HTTP POST using RestTemplate.
[wp_ad_camp_3]
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 | package com.turreta.soademo.clientservice; import com.turreta.soademo.common.dto.AppConfigDTO; import org.springframework.http.HttpEntity; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class ClientController { @RequestMapping("/get-config-from-config-server/{app-name}") public AppConfigDTO getRemoteConfig(@PathVariable("app-name") String appName) { RestTemplate restTemplate = new RestTemplate(); AppConfigDTO dto = new AppConfigDTO(); dto.setApplicationName(appName); HttpEntity<AppConfigDTO> request = new HttpEntity<>(dto); String fooResourceUrl = "http://localhost:8080/get-config-by-name"; ResponseEntity<AppConfigDTO> response = restTemplate.postForEntity(fooResourceUrl,request, AppConfigDTO.class); return response.getBody(); } } |
HTTP GET, PUT, and DELETE
This post demonstrates the use of HTTP POST using RestTemplate.Here are code snippets when using the other 3 important HTTP methods.
[wp_ad_camp_4]
HTTP GET
1 | AppConfig config = restTemplate.getForObject(fooResourceUrl + "/1", AppConfig.class); |
HTTP PUT
1 | restTemplate.put(fooResourceUrl,request, AppConfigDTO.class); |
HTTP DELETE
1 | restTemplate.delete(entityUrl); |
Demo Test
[wp_ad_camp_5]
Download the Codes
The codes are available on GitHub.com and are 3 Maven projects in a single repository. You may need to build them individually first starting with the DTO project. Please see Build/Install Maven Projects in Separate Directors in Travis CI via GitHub
Download from
https://github.com/Turreta/spring-rest-template-demo