我在 Controller 参数上使用 Swagger 注释。因此,我最终得到像 @ApiParam(name="default name", value="this is a default value")
这样的注释。我认为这非常冗长。我想将其更改为类似 @Foo
的内容。我想知道是否有办法在编译时用 @ApiParam
替换 @Foo
。另外,由于我使用的是 Spring,我想我还必须考虑 Spring 中的注释处理顺序。我的意思是,在 Swagger 或 Spring 选择它之后,我不应该用 @Foo
替换 @ApiParam
。有什么办法可以做到这一点吗?
简单来说,我使用相同的注释和相同的参数使用了 5 次。基本上,我想用一些自定义注释替换它们。
我知道我必须展示我已经尝试过的内容,但我不知道从哪里开始。
此外,该问题与 Swagger 无关,它只是一个示例。我想在编译时用另一个注释替换一个注释,这样 Spring 拾取的注释就不会是我放在源代码中的注释,而是我替换的注释。
最佳答案
如果我理解您的要求,那么无需编译时注释处理就可以做到这一点。它并不漂亮,而且可能比它的值(value)更复杂,但这是一种方法。
这是我制作的自定义注释,用于我的速记 @ApiParam
。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface GameIdParam {
String name() default "My Game ID";
String value() default "The integer ID of a particular game";
}
您可以在 @ApiParam
中定义您想要覆盖的任何属性。然后你可以使用Springfox's Extension Framework为新注释实现自定义处理程序。
import com.google.common.base.Optional;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import springfox.documentation.schema.Example;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.EnumTypeDeterminer;
import springfox.documentation.spi.service.contexts.ParameterContext;
import springfox.documentation.spring.web.DescriptionResolver;
import springfox.documentation.swagger.readers.parameter.ApiParamParameterBuilder;
import java.util.function.Predicate;
import static java.util.Optional.ofNullable;
import static springfox.documentation.swagger.common.SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER;
import static springfox.documentation.swagger.common.SwaggerPluginSupport.pluginDoesApply;
import static springfox.documentation.swagger.readers.parameter.Examples.examples;
@Component
public class ShorthandAnnotationPlugin extends ApiParamParameterBuilder {
private final DescriptionResolver descriptions;
private final EnumTypeDeterminer enumTypeDeterminer;
@Autowired
public ShorthandAnnotationPlugin(
DescriptionResolver descriptions,
EnumTypeDeterminer enumTypeDeterminer) {
super(descriptions, enumTypeDeterminer);
this.descriptions = descriptions;
this.enumTypeDeterminer = enumTypeDeterminer;
}
@Override
public void apply(ParameterContext context) {
Optional<GameIdParam> gameIdParam = context.resolvedMethodParameter().findAnnotation(GameIdParam.class);
if (gameIdParam.isPresent()) {
GameIdParam annotation = gameIdParam.get();
// Instantiate an ApiParam so we can take default values for attributes we didn't override.
ApiParam parentAnnotation = AnnotationUtils.synthesizeAnnotation(ApiParam.class);
context.parameterBuilder().name(ofNullable(annotation.name())
.filter(((Predicate<String>) String::isEmpty).negate()).orElse(null))
.description(ofNullable(descriptions.resolve(annotation.value()))
.filter(((Predicate<String>) String::isEmpty).negate()).orElse(null))
.parameterAccess(ofNullable(parentAnnotation.access())
.filter(((Predicate<String>) String::isEmpty).negate())
.orElse(null))
.defaultValue(ofNullable(parentAnnotation.defaultValue())
.filter(((Predicate<String>) String::isEmpty).negate())
.orElse(null))
.allowMultiple(parentAnnotation.allowMultiple())
.allowEmptyValue(parentAnnotation.allowEmptyValue())
.required(parentAnnotation.required())
.scalarExample(new Example(parentAnnotation.example()))
.complexExamples(examples(parentAnnotation.examples()))
.hidden(parentAnnotation.hidden())
.collectionFormat(parentAnnotation.collectionFormat())
.order(SWAGGER_PLUGIN_ORDER);
}
}
@Override
public boolean supports(DocumentationType documentationType) {
return pluginDoesApply(documentationType);
}
}
我用了Springfox's ApiParamParameterBuilder举个例子。
现在,我可以使用我的@GameIdParam
@PostMapping("/{gameId}/info")
public String play(@GameIdParam @PathVariable int gameId) // ...
此模式可以推广到与一系列自定义速记注释一起使用。它并不漂亮,而且引入了另一个间接级别,了解 Springfox Swagger 的人不会熟悉。
希望有帮助!祝你好运!
关于java - 在Spring编译期间用另一个注释替换一个注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57183887/