J123
J123

Reputation: 61

How to develop a REST Webservice method which accepts multiple URI

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.

Upvotes: 0

Views: 165

Answers (2)

MC Ninjava
MC Ninjava

Reputation: 244

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

Upvotes: 0

Alberto
Alberto

Reputation: 795

Modify URI:

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

}

Upvotes: 1

Related Questions