2

Suppose we have a simple spring listener:

@RestController
    public class App {
        /*Listener*/
        @RequestMapping(value = "/{String Value}", method = //some method )
        public String retVal( //PathVariables ) {
                 //code
        }

In this case String Value is a predefined string.


Is there any way to create a listener that has a variable value attach to the URL?

  • `private static final String STRING_VALUE = "/{String Value}"`? – Eugene Aug 29 '18 at 11:03
  • 3
    In this [way](https://stackoverflow.com/questions/37513117/how-do-i-make-controller-mapping-path-configurable), maybe? – m4gic Aug 29 '18 at 11:04

3 Answers3

0

Like this you mean?

    @RequestMapping(value = "/URL/{aParam}")
    public void aMethod(
        @PathVariable(value = "aParam") String aParam)
 {
        ...
    }
Vipin CP
  • 3,642
  • 3
  • 33
  • 55
0

You can use @PathVariable to do this.

@RestController
public class App {
   @RequestMapping(value = "/{StringValue}", method = //some method )
   public String retVal(@PathVariable(value = "StringValue") String StringValue) {
      System.out.println(StringValue); //it will print StringValue
   }
}

Refer this How do I make @Controller mapping path configurable?

Alien
  • 15,141
  • 6
  • 37
  • 57
0

As other have pointed out you can use Spring PathVariable annotation.

Additionally if the variable name you defined in the RequestMapping match the method argument name, you can write just @PathVariable instead of @PathVariable(value = "variable").

@RestController
public class App {

    @RequestMapping(value = "/{variable}"/*, method = some method */)
    public String retVal(@PathVariable String variable) {
       //code
    }
}
senjin.hajrulahovic
  • 2,961
  • 2
  • 17
  • 32