开发者

Why does Spring allow controller annotated request mappings on private methods?

Just came accross this today in a Spring MVC cotnroller class,

    @RequestMapping(value = { "/foo/*" }, method = { RequestMethod.GET})
    private String doThing(final WebRequest request) {
        ...
 开发者_JAVA百科       return "jsp";
    }

This is making it a bit harder to write a test, I'll probably change it to public but what's the point of allowing mappings on private methods?


Java does not provide a mechanism for limiting the target of annotations based on access modifier.


As @smp7d stated, Java does not limit the target of annotations based on access modifiers, but syntactically speaking, @RequestMapping should not work on private methods. Also we cannot limit this, since it would break the backward compatibility. So, you can either go for defining your methods as public or you can create your own custom implementation.

Take a look at this: Spring's @RequestMapping annotation works on private methods

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜