是否可以使用预定义的参数集创建与Spring MVC @RequestMapping等效的Java注释?
例如,可以让我简单地使用以下内容:
@PostJson("/input")
public Output myMethod(Input input) {
代替:
@RequestMapping(value = "/input",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
public Output myMethod(Input input) {
更新:让我尝试更深入地研究。AFAIK,当扫描bean时,Spring似乎能够处理元注释。例如,如果我创建一个批注,则说@MyComponent,然后用@Component批注:
@Component
public @interface MyComponent {
String value() default "";
}
Spring似乎能够使用@MyComponent查找Bean并识别@MyComponent中的参数(在这种情况下为值),就像它们来自@Component一样。
@MyComponent("MyBean")
public class SomeClass {
我已经尝试过@RequestMapping的类似策略
@RequestMapping(method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
public @interface PostJson {
String value() default "";
}
固定参数(方法产生)似乎是正确的,但是可变参数(值)将被忽略。
我希望这不是特定于@Component的功能,并且可以将其与@RequestMapping一起使用。
不容易,不。该@RequestMapping
注释是依赖于RequestMappingHandlerMapping
和RequestMappingHandlerAdapter
。当您提供<mvc:annotation-driven />
或时,这是默认情况下注册的MVC堆栈的两个元素@EnabledWebMvc
。他们只扫描@Controller
和@RequestMapping
,没有其他内容。
要使自己的注释生效,您必须重写(并注册)这两个注释或从头开始创建新的注释,以提供对处理程序方法的扫描和处理。您可以从这些类和其他HandlerMapping
实现中获得一些启发,但这确实不是一个简单的任务。
或者,您可能希望研究可以与Spring很好地集成(但不一定是Spring MVC)的Java Restful Web Services。当您确切知道自己想要什么时,它可以提供一些不那么肿的映射注释。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句