svpace
svpace

Reputation: 35

Create annotation setting @RequestMapping parameters using Spring MVC

Is it possible to create a Java annotations equivalent to a Spring MVC @RequestMapping with a predefined set of parameters?

For instance, something that allows me to simply use:

@PostJson("/input")
public Output myMethod(Input input) {

instead of:

@RequestMapping(value = "/input",
    method = RequestMethod.POST,
    produces = MediaType.APPLICATION_JSON_VALUE, 
    consumes = MediaType.APPLICATION_JSON_VALUE)
public Output myMethod(Input input) {

UPDATE: Let me try to dig a little deeper. AFAIK, Spring seems to be able to handle meta-annotations when scanning for beans. For instance, if I create an annotation, lets say @MyComponent, and annotate it with @Component:

@Component
public @interface MyComponent {
    String value() default "";
}

Spring seems able to find beans with @MyComponent and to recognize the parameters (value in this case) in @MyComponent as if they were from @Component

@MyComponent("MyBean")
public class SomeClass {

I've tryed similar tactic with @RequestMapping

@RequestMapping(method = RequestMethod.POST, 
    produces = MediaType.APPLICATION_JSON_VALUE)
public @interface PostJson {
    String value() default "";
}

The fixed parameters (method an produces) seems to be correct, nonetheless, the variable parameter (value) is ignored.

I am hoping that this is not a @Component specific feature and that I could use it with @RequestMapping.

Upvotes: 2

Views: 1538

Answers (2)

Rossen Stoyanchev
Rossen Stoyanchev

Reputation: 5008

While this isn't currently supported out of the box but will be (thanks for creating https://jira.spring.io/browse/SPR-12296!), it isn't hard to do. If you look in RequestMappingHandlerMapping the protected method getMappingForMethod accepts a method and returns a RequestMappingInfo populated with information from the type + method-level @RequestMapping annotations. You can however populate this RequestMappingInfo from anything, e.g. your own annotation, or some other source (external routes configuration). Just follow the example of the code there.

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

Not easily, no. The @RequestMapping annotation is tied to RequestMappingHandlerMapping and RequestMappingHandlerAdapter. These are two elements of the MVC stack registered by default when you provide <mvc:annotation-driven /> or @EnabledWebMvc. They only scan for @Controller and @RequestMapping, nothing else.

To make your own annotation work, you'd have to override (and register) these two or create new ones from scratch which provide the scanning and handling of handler methods. You can get some inspiration from those classes and other HandlerMapping implementations, but it really isn't a simple task.

You might, alternatively, want to look into Java Restful Web Services which can integrate quite well with Spring (not necessarily Spring MVC though). It can provide some less bloated mapping annotations when you know exactly what you want.

Upvotes: 3

Related Questions