Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a PathVariable of a controller specified at class level in Spring?

Can I do something like this with Spring MVC ?

@RequestMapping(value = "/{root}")
public abstract class MyBaseController {

    @PathVariable(value = "root")
    protected ThreadLocal<String> root;

}

@Controller
public class MyController extends MyBaseController {

    @RequestMapping(value = "/sayHello")
    @ResponseBody
    public String hello() {
        return "Hello to " + this.root.get();
    }

}

When I request to http://..../roberto/sayHello, I get this as response:

Hello to roberto
like image 606
Beto Neto Avatar asked Aug 26 '15 20:08

Beto Neto


1 Answers

According to the docs:

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/PathVariable.html

the PathVariable annotation is itself annotated with @Target(value=PARAMETER) so it shouldn't be possible to be used the way you're saying as it's only applicable to method parameters.

like image 74
Alfonso Presa Avatar answered Nov 15 '22 20:11

Alfonso Presa