-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Custom @RequestMapping annotations [SPR-12296] #16901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Rossen Stoyanchev commented Good suggestion. Ahead of 4.2 as a workaround you could override the getMappingForMethod method in RequestMappingHandlerMapping and return a RequestMappingInfo populated from your own custom annotation(s). |
Joseph commented This feature also require supporting multiple Suppose i have an annotation which detect a post request, and another annotation for producing json results, i should be able to use both of them on a single handler method, and the framework should combine the configuration of both. For example I should be able to have an annotation like: @RequestMapping(method = RequestMethod.POST)
public @interface Post {
String value() default "";
} and another one like: @RequestMapping(
produces = MediaType.APPLICATION_JSON_VALUE
consumes = MediaType.APPLICATION_JSON_VALUE)
public @interface Json {
String value() default "";
} and can be used together on the same handler method like: @Post
@Json
public Output myMethod(Input input) { |
Sérgio Vale e Pace commented I believe that supporting multiple Supporting multiple On the other hand, supporting just the meta-annotations, maybe throwing an error if more than one meta-annotation is detected, would already be of great value since, at least for my use-case, there area only three or four configurations shared by most of the methods, therefore, the mutually exclusive |
Rossen Stoyanchev commented Joseph what you're proposing makes sense indeed but are some further considerations and qualifications. Presently both type- and method-level The second point is there are existing rules about combining type- and method-level annotations as documented on |
Rossen Stoyanchev commented Sérgio Vale e Pace, funny we each posted at almost identical times :) I like your proposal to keep those as incremental, separate steps. First adding meta annotations and then support for aggregating them along the lines I outlines above. Something else to keep in mind is there a number of tickets to improve support for composable and meta annotations (see #16020 and then the list of related tickets, /cc Sam Brannen). It makes even more sense to split these two concerns since the second may be need to be coordinated with those related related tickets. This is not to say we can't do both. It's simply splitting them up. Joseph, would you mind creating a separate ticket? |
Rossen Stoyanchev commented In order to support |
Sam Brannen commented Hi guys, Did anyone ever introduce a new JIRA issue to track the request for supporting aggregated declarations of If so, please let me know the issue. If not, please create one! ;) Thanks, Sam |
Sam Brannen commented If you're following this issue, you will likely be interested in #18022 and the new spring-composed project. ;) |
Edwin Dalorzo commented I was trying this feature in the latest release of Spring, I think this still does not work as the original proposal suggested. For example, if I define an annotation like this: @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@RequestMapping(
method = RequestMethod.GET,
produces = {MediaType.APPLICATION_JSON_VALUE},
consumes = {MediaType.APPLICATION_JSON_VALUE}
)
public @interface GET {
String value() default "";
} And then I use it in a controller, like this: @GET("/foo/{id}")
public SomeModel foo(@PathVariable Integer id){
//...
} This does not get properly mapped. Actually for this to work I would have to either burn the path in the new meta-annotation or define yet another Ideally the value of my |
Sam Brannen commented Edwin Dalorzo, Spring does not support overriding the Instead, you have to use See @Get from the Spring Composed project for a concrete working example. Regards, Sam |
Sam Brannen commented In other words, the following works just fine. @Retention(RUNTIME)
@Target(METHOD)
@RequestMapping(method = GET)
public @interface Get {
@AliasFor(annotation = RequestMapping.class, attribute = "path")
String[] value() default {};
}
@Get("/foo/{id}")
public SomeModel foo(@PathVariable Integer id) {
// ...
} Note, however, that the return type of the |
Sérgio Vale e Pace opened SPR-12296 and commented
Support creating annotations with
@RequestMapping
meta-annotation like it is possible to do with the@Component
annotation.For instance, I would like to create an annotation
@PostJson
that would replace a@RequestMapping
with parameters:So I could write:
instead of:
Affects: 4.1.1
Reference URL: http://stackoverflow.com/questions/26049810/create-annotation-setting-requestmapping-parameters-using-spring-mvc/26050717#26050717
Issue Links:
@RequestMapping
1 votes, 6 watchers
The text was updated successfully, but these errors were encountered: