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

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

问题描述

我正在使用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 members have only limited types (JLS 9.6).

如果在注释类型中声明的方法的返回类型不是以下之一,则它是编译时错误:基本类型,String,Class,Class的任何参数化调用,枚举类型(第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).该标准使用术语相称.

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是原始类型或String,而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天全站免登陆