将多个注释合二为一以避免重复 [英] Combine multiple annotations into one to avoid repeating them

查看:33
本文介绍了将多个注释合二为一以避免重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring MVC 实现各种 REST 服务.对于文档,我使用的是 Swagger.

I'm working on the implementation of various REST services, using Spring MVC. For documentation, I'm using Swagger.

这很好用,文档看起来不错,而且非常实用.我唯一的问题是文档的注释确实挤满了控制器类,尤其是错误代码注释.

This works nice and the documentation looks good and is really functional. The only problem I have is that the annotations for documentation really crowd the controller classes, especially the error code annotations.

示例:

@ApiErrors(value = {
    @ApiError(code = 123, reason = "Reason123"),
    @ApiError(code = 124, reason = "Reason124"),
    @ApiError(code = 125, reason = "Reason125"),
    @ApiError(code = 126, reason = "Reason126"),
    @ApiError(code = 127, reason = "Reason127") })
public void exampleFunctionImplementation() {
}

在许多情况下,这会导致大量注释,其中真正的应用程序代码隐藏在两者之间.另外,这个注解集经常重复,因为很多方法可能返回相同的错误码集.

In many cases, this leads to large blocks of annotations where the real application code is hidden somewhere in between. In addition, this annotation sets are often repeated, as a lot of methods may return the same set of error codes.

是否有任何选项可以通过将其他地方的注释列表定义为另一个类文件中的常量来缩短这一点?或者我可能忽略了一些更简单的东西?

Is there any option to shorten this a bit through defining the annotation list somewhere else as a constant in another class file? Or maybe something even more simple I may have overlooked?

我尝试在某处定义 @ApiError 项的数组,但这不会编译:

I tried with defining the array of @ApiError items somewhere, but this won't compile:

ApiError[] array = {ApiError(code = 123, reason = "Reason123")};

如果有人能给我提示如何解决这个问题,我会很高兴,在此先感谢!

I would be glad if anybody could give me a hint how to solve this problem, thanks in advance!

推荐答案

Annotation 成员只有有限的类型 (JLS 9.6).

Annotation members have only limited types (JLS 9.6).

如果在注释类型中声明的方法的返回类型不是以下之一,则为编译时错误:原始类型、字符串、类、类的任何参数化调用、枚举类型(第 8.9 节)、注释类型或元素类型为上述类型之一的数组类型(第 10 节).

It is a compile-time error if the return type of a method declared in an annotation type is not one of the following: a primitive type, String, Class, any parameterized invocation of Class, an enum type (§8.9), an annotation type, or an array type (§10) whose element type is one of the preceding types.

它们的值必须是常量表达式 (JLS 9.7).该标准使用术语commensurate.

Their values must be constant expressions (JLS 9.7). The standard uses the term commensurate.

T 是一个数组类型 E[] 并且:

T is an array type E[] and either:

V 是一个 ElementValueArrayInitializer,V 中的每个 ElementValue(类似于数组初始值设定项中的 VariableInitializer)与 E 相称;或

V is an ElementValueArrayInitializer and each ElementValue (analogous to a VariableInitializer in an array initializer) in V is commensurate with E; or

V 是与 E 相称的 ElementValue.

V is an ElementValue that is commensurate with E.

V 的类型与 T 赋值兼容(第 5.2 节),此外:

The type of V is assignment compatible (§5.2) with T, and furthermore:

如果 T 是原始类型或字符串,并且 V 是常量表达式(第 15.28 节).

If T is a primitive type or String, and V is a constant expression (§15.28).

V 不为空.

如果 T 是 Class 或 Class 的调用,并且 V 是类文字(第 15.8.2 节).

If T is Class, or an invocation of Class, and V is a class literal (§15.8.2).

如果 T 是枚举类型,而 V 是枚举常量.

If T is an enum type, and V is an enum constant.

您的数组不是常量表达式,因此您的代码无法编译.如果您预计有大型注释列表,也许还有另一种方法可以完成此任务.我不了解 Swagger,因此您可能无法避免这种情况.

Your array is not a constant expression, so your code won't compile. If you anticipate having large annotation lists, perhaps there is another way to do this task. I don't know Swagger, so you might not be able to avoid this, however.

这篇关于将多个注释合二为一以避免重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆