Custom #RequestMapping annotation - spring

I have few methods in my spring controllers which are mapped on the same path, example.
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
protected ResourceDTO getById(#PathVariable int id) {
return super.getById(id);
}
I was wondering if there is a way to create an annotation that will automatically have set value and method, to have something like this:
#RequestMappingGetByID
protected ResourceDTO getById(#PathVariable int id) {
return super.getById(id);
}
Have a nice day everyone
Update
The goal of this is the following
all my controllers (eg. user, order, client) extends a parametrized BaseController that includes a base set of function (get by id, save, update, delete, etc) All the logic is on the BaseController, but in order to map the value I have to add the annotation on the specific controller.
Instead of writing all the time {id} and post I would like to annotate the methods with a custom interface that already includes those values

The following works for Spring 4.1.x that I tested:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
#interface RequestMappingGetByID {
}
Then you can use
#RequestMappingGetByID
protected ResourceDTO getById(#PathVariable int id) {
return super.getById(id);
}
like you mention.
This kind of annotation is was Spring calls a meta-annotation. Check out this part of the documentation
I am not sure if this meta-annotation would work in versions of Spring prior to 4.x, but it's definitely possible since Spring had some meta-annotation handling capabilities in the 3.x line
If you where using Groovy, you could also take advantage of the #AnnotationCollector AST, which in effect would keep the duplication out of your source code, but would push the regular #RequestMapping annotation into the produced bytecode. Check out this for more details.
The benefit in this case would be that Spring need not have to be equipped with the meta-annotation reading capabilities, and there for the solution possibly works on older Spring versions

Related

Automatically document #PathVariable annotated parameters within #ModelAttribute annotated methods

In our REST-API we need to be multi-tenant capable. For achiving this all rest controllers subclass a common REST controller which defines a request mapping prefix and exposes a model attribute as follows
#RequestMapping(path = "/{tenantKey}/api")
public class ApiController {
#ModelAttribute
public Tenant getTenant(#PathVariable("tenantKey") String tenantKey) {
return repository.findByTenantKey(tenantKey);
}
}
Derived controllers make use of the model attributes in their request mapping methods:
#RestController
public class FooController extends ApiController {
#RequestMapping(value = "/foo", method = GET)
public List<Foo> getFoo(#ApiIgnore #ModelAttribute Tenant tenant) {
return service.getFoos(tenant);
}
}
This endpoint gets well documented in the swagger-ui. I get an endpoint documented with a GET mapping for path /{tenantKey}/api/foo.
My issue is, that the {tenantKey} path variable is not documented in swagger-ui as parameter. The parameters section in swagger is not rendered at all. If I add a String parameter to controller method, annotating it with #PathVariable("tenantKey) everything is fine, but I don't want a tenantKey parameter in my controller method, since the resolved tenant is already available as model attribute.
So my question is: Is there a way do get the #PathVariable from the #ModelAttriute annotated method in ApiController documented within swagger-ui in this setup?
Project-Setup is
Spring-Boot (1.4.2)
springfox-swagger2 (2.6.1)
springfox-swagger-ui (2.6.1)
This is certainly possible. Model attributes on methods are not supported currently. Instead, you could take the following approach.
Mark the getTenant method with an #ApiIgnore (not sure if it gets treated as a request mapping.)
In your docket you can add tenantKey global path variable (to all end points). Since this is a multi-tenant app it's assuming this applies to all endpoints.

Using SimpleFormController

I have a controller which accepts a form post -
#Controller
public class RegistrationFormController extends SimpleFormController {
.....
.....
#RequestMapping(value="index", method=RequestMethod.POST)
protected ModelAndView onSubmit(#ModelAttribute Registration registration) throws Exception {
String uname=registration.getUsername();
.....
.......
ModelAndView mv = new ModelAndView("success");
.....
......
mv.addObject("addr",addr);
return mv;
}
Thsi would work just as well even if i do not extend SimpleFormController.
What then can I gain by extending?
Well, the setup seems not good. The #Controller annotation is a nice feature which is used for declaring stereotypes. It just says that it is another spring Component or Spring managed bean and can be detected in component scan.
Where as when you extend SimpleFormController you explicitly say that it is a Controller and it has to be used as a controller, it will be used to accept submitted form data and return a response in form of a view.
The two notations in the same class makes no sense at all, I feel that making a class SimpleFormController restricts it from using any method name and you are forced to use a onSubmit method. Whereas, if you use #Controller you leverage all the flexibility in Spring 3 and above.

Duplicate RequestMapping

I'm working on an open source named OpenMRS supporting Spring MVC. I cannot modify core source for update purpose later. So I must write a module, something like plugin to add functions to the system. The problem is that I want to alter the original screen to mine by using portlet to redirect to my jsp. The controller of the core code is something like this:
#RequestMapping("/patientDashboard.form")
protected String renderDashboard(#RequestParam(required = true, value = "patientId") Integer patientId, ModelMap map){
....
return "patientDashboardForm";
}
I'm not familiar with Spring but as I know that when the url ends with /patientDashboard.form?patientId=xxx the function will call patientDashboardForm.jsp. Now I want to return to my jsp so I must define a new class with same code but return to my jsp (to do this because cannot modify the core code). But by defining same mapping /patientDashboard.form causes error "Cannot map handler XXX to URL path /patientDashboard.form: There is already handler YYY mapped".
So is there anyway to overcome this situation ?
There is no way to overrule an existing #RequestMapping. Each mapping must be unique. What you could do is the following. Instead of adding a request parameter, add a path parameter like this
#RequestMapping("/patientDashboard.form/{patientId}", method = RequestMethod.GET)
public String renderDashboard(#PathVariable("patientId") final long id, Model model) {
/* your code here */
}
This will create a new #RequestMapping that will differ from the existing one.
You have to create another #Controller extending the existing one. Then, you can define your custom mappings (you can't reuse the existing one) and reimplement the superclass methods at your convenience, redirecting to your view and defining custom logic there.
Example:
#Controller
#RequestMapping("/your_new_mapping")
public class YourController extends BaseController {
#Override
#RequestMapping("/patientDashboard.form")
public void renderDashboard(#RequestParam(required = true, value = "patientId") Integer patientId, ModelMap map){
// Call to default functionallity
super.renderDashboard(patientId, map);
...
// your custom code here
return "yourCustomJSPHere";
}
}

Non JSR303 equivalent of #Valid annotation in Spring 3.x?

Plain and simple issue:
I would like to annotate my method parameter with some sort of annotation that would fire regular spring validation mechanism (based on Validator interface)
I don't want to include JSR303 dependency
Any ideas? I looked on #Validated but it seems that it was not created for this purpose.
Right now I do it like this:
public String req(#ModelAttribute SomeRequest request, BindingResult errors) {
validator.validate(request, errors); // This can be avoided
if (!errors.hasErrors()) {
// Valid request
return ...
} else {
// There were errors
return ...
}
}
In that case you need to create custom annotation using #interface and then use that instead of standard #Valid annotation and at runtime identify it and validate your fields accordingly. Hope this helps you. Cheers.

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) {
...
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

Resources