【发布时间】:2020-05-20 08:38:46
【问题描述】:
如果我在 Spring 的控制器中有一个映射,例如:
@RequestMapping(params = "foo", method = RequestMethod.GET)
public String findAllBars(@RequestParam(value = "amount", defaultValue = "10") int amount, Model uiModel) {
我可以做一个注解,把上面的默认值10封装起来吗?喜欢:
@RequestMapping(params = "foo", method = RequestMethod.GET)
public String findAllBars(@MyAmountAnnotation int amount, Model uiModel) {
让 spring 按预期理解我的注释。我找到了https://stackabuse.com/spring-annotations-requestmapping-and-its-variants/,当我看到他们对@GetMapping、@PostMapping 等所做的事情时,我有点希望。
但是我在尝试时遇到编译错误“RequestParam not applicable to annotation type”:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestParam(value = "amount", defaultValue = "10")
public @interface MyAmountAnnotation {
//...
我可以这样做以将详细注释“封装”到专门的注释吗?怎么了?
【问题讨论】: