Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rest service pass through via spring

I have built spring application, i have one requirement to consume a rest service and just expose the same via my spring application because of some security

i.e for the rest service i consume i have provide security credentials that only my application should know and expose the same service via my spring application.

I could write wrapper rest service for every rest service i consume with corresponding authentication and expose these wrapper services which can be authenticated with my spring application Auth credentials

But this is lot of work what i am essentially doing is consuming webservice and expose the same with some auth mapping. In spring is there a option to pass through the rest services

like image 851
Navigator Voyager Avatar asked Jan 30 '13 01:01

Navigator Voyager


People also ask

Is spring boot GOOD FOR REST API?

Advantages of using Spring BootIt allows you to create REST APIs with minimal configurations. A few benefits of using Spring Boot for your REST APIs include: No requirement for complex XML configurations. Embedded Tomcat server to run Spring Boot applications.

How do I use RESTful webservice in spring boot?

Create a simple Spring Boot web application and write a controller class files which is used to redirects into the HTML file to consumes the RESTful web services. We need to add the Spring Boot starter Thymeleaf and Web dependency in our build configuration file. For Maven users, add the below dependencies in your pom.

Can we use RestController in Spring MVC?

Spring RestController annotation is used to create RESTful web services using Spring MVC. Spring RestController takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON or XML response.


1 Answers

Why not just expose a REST service for each HTTP request type, and route it based on information in the path? For example (untested, probably won't work as is, but you get the basic idea):

@Autowired
RestTemplate restTemplate;

@Value("${rest.proxy.target.base.url}")
String targetBaseUrl;

@RequestMapping(value = "/restProxy/{restUrlPath}", method = RequestMethod.GET)
public @ResponseBody String restProxyGet(@PathVariable("restUrlPath") String restUrlPath) {
    return restTemplate.getForObject(targetBaseUrl+ "/" + restUrlPath, String.class);
}

@RequestMapping(value = "/restProxy/{restUrlPath}", method = RequestMethod.POST)
public @ResponseBody String restProxyPost(@PathVariable("restUrlPath") String restUrlPath, @RequestBody String body) {
    return restTemplate.postForObject(targetBaseUrl + "/" + restUrlPath, body, String.class);
}

//Can also add additional methods for PUT, DELETE, etc. if desired

If you need to communicate with different hosts you can just add another path variable that acts as a key to a map that stores the different target base URLs. You can add whatever authentication you want from the controller, or via custom authentication in Spring Security.

Your question is somewhat light on details so your specific scenario may complicate things, but the basic approach should work.

like image 126
ach Avatar answered Nov 03 '22 00:11

ach