0

How to develop a REST Webservice method which accepts multiple URIs for example

If we have a method

@RequestMapping(/add)
 public Response add(@RequestParam List elements){
  }

The method above serves for URL https://local host:8080/add

  1. I have asked in interview how do we have a single Webservice which serves multiple URIs which I was not able to answer as I thought we should have unique URIs for each method

  2. A follow up to this how do we have a method which returns the format whatever choosen (JSON, XML, PLAIN TEXT) from postman client.

J123
  • 61
  • 2
  • 11
  • Please take a look on [Multiple Spring @RequestMapping annotations](https://stackoverflow.com/questions/2513031/multiple-spring-requestmapping-annotations) – Volodymyr Demenkov Apr 24 '18 at 10:04
  • You should be able to use `@RequestMapping(path={"/add", "/delete"})` and you can include `produces={"application/json", "text/xml"}`. – daniu Apr 24 '18 at 10:06

2 Answers2

1

Modify URI:

@RequestMapping(value = "/", produces = {
            MediaType.APPLICATION_JSON_VALUE,
            MediaType.APPLICATION_XML_VALUE
            })
public Response add(@RequestParam List elements){

}
Alberto
  • 745
  • 1
  • 6
  • 25
0

For the first question there is many ways to do that Spring restTemplate can allow you to call a rest service in another method so all you have to do is to call your rest service when another uri is requested You can also use proxies (web server proxies) to map same webservice to many URIs

For the second one you need to pass format as parameter and call specific service linked to format when executing

Hope that helps

MC Ninjava
  • 214
  • 2
  • 7