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?
A @RequestMapping on the class level is not required. Without it, all paths are simply absolute, and not relative.
Using @RequestMapping With HTTP Methods In the code snippet above, the method element of the @RequestMapping annotations indicates the HTTP method type of the HTTP request. All the handler methods will handle requests coming to the same URL ( /home), but will depend on the HTTP method being used.
annotation. RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.
The @GetMapping annotation assigns specified handler methods to HTTP GET requests. @RequestMapping(method = RequestMethod. GET) is a constructed annotation that serves as a shorthand for @RequestMapping(method = RequestMethod.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With