Multiple requestmapping value with path variables

前端 未结 4 1881
北海茫月
北海茫月 2021-02-03 10:08
@RequestMapping(value = {\"/abcd\", \"/employees/{value}/{id}\"})
public String getEmployees(
      @PathVariable(value = \"value\") String val, 
      @PathVariable(val         


        
相关标签:
4条回答
  • 2021-02-03 10:40

    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.).

    0 讨论(0)
  • 2021-02-03 10:45

    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
    0 讨论(0)
  • 2021-02-03 10:48

    I had a similar problem and I found this solution:

    @GetMapping(value = {"/clients/page", "/clients/page/{page}"})
    public Page<Client> index(@PathVariable Optional<Integer> page) {
       PageRequest pageRequest = page.map(integer -> PageRequest.of(integer, 4))
       .orElseGet(() -> PageRequest.of(0, 4));
          return clientService.showAll(pageRequest);
    }
    

    Intellij helped me to get this kind of compact result. Although Intellij throw this message:

    ''Reports any uses of java.util.Optional, java.util.OptionalDouble, java.util.OptionalInt, java.util.OptionalLong or com.google.common.base.Optional as the type for a field or a parameter. Optional was designed to provide a limited mechanism for library method return types where there needed to be a clear way to represent "no result". Using a field with type java.util.Optional is also problematic if the class needs to be Serializable, which java.util.Optional is not. ''

    To be honest, I'm new in this business and I don't understand clearly what the IDE is telling me. If someone with more expertise could help to clarify that message would be great.

    0 讨论(0)
  • 2021-02-03 10:50

    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

    0 讨论(0)
提交回复
热议问题