Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple requestmapping value with path variables

@RequestMapping(value = {"/abcd", "/employees/{value}/{id}"})
public String getEmployees(
      @PathVariable(value = "value") String val, 
      @PathVariable(value = "id") String id,
      @RequestParam(value = "param", required = false) String value) {
        // ********

}

For one url I am passing the path variable and for one I am not. But I want both the url to hit the same API. How can I achieve it?

like image 264
Sharique Avatar asked Jul 28 '15 05:07

Sharique


People also ask

How do I apply multiple path variables in Spring boot?

Try this: @RequestMapping(value = "/{lang}/{count}/{term}", method=RequestMethod. GET) public ResponseEntity<?> getSomething(@PathVariable("lang") String lang, @PathVariable("count") String count, @PathVariable("term") String term) { // Your code goes here. }

Can you have multiple path variables?

Luckily, it is quite easy in Spring to configure multiple path variables. All you need to do is to adapt the mapping path and to annotate all method arguments whose values are expected from the user. In this example, we configured two path variables.

Can we use path variable in post mapping?

To edit the path variable, click on Params to see it already entered as the key. Update the value as needed. For example, :entity can be “user” in this specific case. Postman also gives you suggestions to autocomplete the URL.

Can we use PathVariable and RequestParam together?

4) Spring MVC allows you to use multiple @PathVariable annotations in the same method, provided, no more than one argument has the same pattern. That's all about the difference between @PathVariable and @RequestParam in Spring MVC.


3 Answers

You can now have optional path variables via support for Java 8 Optional. At least Spring version 4.x will be required.

@RequestMapping({"/abcd", "/employees/{value}/{id}"})
public String getEmployees(
    @PathVariable("value") Optional<String> val, 
    @PathVariable("id") Optional<String> id,
    @RequestParam("param") Optional<String> value
) {
    // ********
}

N.B. this doesn't work with the optional primitives (OptionalInt, etc.).

like image 146
OrangeDog Avatar answered Oct 19 '22 01:10

OrangeDog


Just found a way to do this without using multiple methods.

First create a simple class to hold the path variables:

public class EmployeesRequest {
  private String value;
  private String id;

  public String getValue() {
    return this.value;
  }

  public void setValue(String value) {
    this.value = value;
  }

  public String getId() {
    return this.id;
  }

  public void setId(String id) {
    this.id = id;
  }
}

Then define your controller method like this:

@RequestMapping(value={
  "/abcd",
  "/employees/{value}/{id}"
})
public String getEmployees(@RequestParam(value="param", required=false) String param,
                           EmployeesRequest request) {
  if (request.getValue() != null) {
    // do something
  } else {
    // do something else
  }
}

Spring will automatically map any path variables available to the EmployeesRequest class. Spring will also do this for any request parameters so you can simplify things further by adding the request parameter to EmployeesRequest:

public class EmployeesRequest {
  private String value;
  private String id;
  private String param;

  public String getValue() {
    return this.value;
  }

  public void setValue(String value) {
    this.value = value;
  }

  public String getId() {
    return this.id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getParam() {
    return this.param;
  }

  public void setParam(String param) {
    this.param = param;
  }
}

And then finally:

@RequestMapping(value={
  "/abcd",
  "/employees/{value}/{id}"
})
public String getEmployees(EmployeesRequest request) {
  if (request.getValue() != null) {
    // do something
  } else {
    // do something else
  }
}

An added benefit of this solution is that now you can support both variables or request parameters. Meaning all of these would be valid:

  • /abcd
  • /abcd?param=123
  • /abcd?value=123&id=456&param=789
  • /employees/123/456
  • /employees/123/456?param=123
like image 5
Kyle Avatar answered Oct 19 '22 01:10

Kyle


We can't have optional path variables, you can have two controller methods which can call the same service.

First Method

@RequestMapping("/abcd")
public String getEmployees(@RequestParam(value="param", required=false)String value){}

Second Method

@RequestMapping("/employees/{value}/{id}")
public String getEmployees(@PathVariable(value="value") String val, @PathVariable(value="id") String id, @RequestParam(value="param", required=false) String value){}

For @RequestParam we can use,

@RequestParam(value="somevalue",required=false)

for optional params rather than a pathVariable

like image 5
Ankur Singhal Avatar answered Oct 19 '22 01:10

Ankur Singhal