Michael Stum
Michael Stum

Reputation: 180954

Can I override the @RequestMapping on a type for a method?

I have a spring controller/POJO like this:

@RequestMapping("/foo");
public class MyController {
    @RequestMapping("/bar") 
    public String MyAction() { return someSharedFunc(false); }

    @RequestMapping("/debug/ping");
    public String MyDebugPing() { return someSharedFunc(true); }

    private String someSharedFunc(boolean debug) {
      if(debug) return "FooBar"; else return "Debug!";
    }
}

In this scenario, the URL for MyDebugPing is /foo/debug/ping. However, I want it to be /debug/ping, effectively ignoring the RequestMapping on the class.

Is that possible?

Upvotes: 17

Views: 9232

Answers (1)

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

Just remove the @RequestMapping annotation from the class and use full paths per individual methods. E.g.

public class MyController {
    @RequestMapping("/foo/bar") 
    public String MyAction() { return someSharedFunc(false); }

    @RequestMapping("/debug/ping");
    public String MyDebugPing() { return someSharedFunc(true); }

    private String someSharedFunc(boolean debug) {
      if(debug) return "FooBar"; else return "Debug!";
    }
}

If there is a lot of methods then you can simply move out the method to another controller.

Upvotes: 11

Related Questions