gpt4 book ai didi

java - 如何在Java中输入def注解

转载 作者:行者123 更新时间:2023-12-01 13:09:37 25 4
gpt4 key购买 nike

我有一组具有非常相似的注释声明的方法,如何缩短它们?不要到处重复它们?

@RequestMapping(value = "/relative_path", 
method = RequestMethod.GET,
consumes = "appilcation/json",
produces = "appilcation/json")
@ResponseBody
public User method1(HttpServletRequest httpServletRequest,
@RequestBody String requestBody)
{...}

最佳答案

您可以为此创建一个 spring 元注释:

@RequestMapping(value = "/relative_path", 
method = RequestMethod.GET,
consumes = "appilcation/json",
produces = "appilcation/json")
@ResponseBody
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}

并在您的方法中使用它:

@MyAnnotation
public User method1(HttpServletRequest httpServletRequest,
@RequestBody String requestBody) { }

解决方案:

@RequestMapping(method = RequestMethod.POST,
consumes = "appilcation/json",
produces = "appilcation/json")
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}

@RequestMapping(value = "/relative_path")
@MyAnnotation
public User method1(HttpServletRequest httpServletRequest,
@RequestBody String requestBody) { }

关于java - 如何在Java中输入def注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22984415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com