Swagger 覆盖路径注释 [英] Swagger overrides Path-Annotations

查看:37
本文介绍了Swagger 覆盖路径注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚大摇大摆地生成了一个有效的 swagger.json.我使用 Application-config 方法配置了招摇.但是,一旦我覆盖 getClasses-Method 以添加招摇资源,我的 JAX-RS 路径注释类就会停止工作.方法是这样的

I just got swagger to produces a valid swagger.json. I configured swagger by using the Application-config method. However, as soon as I override the getClasses-Method to add the swagger resouces, my JAX-RS Path-annotated classes stop working. The method looks like this

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> resources = new HashSet<>();

    resources.add(io.swagger.jaxrs.listing.ApiListingResource.class);
    resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);

    return resources;
}

并调用 super.getClasses() 返回一个空集.我的项目中有太多资源,我不想手动添加.

and invoking super.getClasses() returns am empty set. I got too many resources in my project, which I would not like to add manually.

swagger 有什么办法不弄乱我之前的配置吗?

Is there any way swagger does not mess with my previous configuration?

谢谢!

推荐答案

你可以使用 javax.ws.rs.core.Feature.只需通过回调的 FeatureContext 注册类.使用 @Provider 注释功能将使其通过扫描注册.

You can use a javax.ws.rs.core.Feature. Just register the classes through the callback's FeatureContext. Annotating the feature with @Provider will have it registered through the scanning.

@Provider
public class SwaggerFeature implements Feature {

    @Override
    public boolean configure(FeatureContext context) {
        context.register(ApiListingResource.class);
        context.register(SwaggerSerializers.class);
        return true;
    }
}

但请注意,如果应用程序已经通过类路径扫描注册资源和提供者,我想它也应该选择 Swagger 类,因为它们使用 @Path 注释[1]@Provider[2].这些是类路径扫描寻找的注释.

But note that if the application is already registering the resources and providers by class-path scanning, I imagine it should also pick up the Swagger classes, as they are annotated with @Path[1] and @Provider[2]. Those are the annotations the class-path scan looks for.

我自己没有尝试过(我停止使用类路径扫描[3]),但是您是否尝试过根本不注册它们?理论上,class-path 扫描应该会拾取它.

I haven't tried it myself (I stopped using class-path scanning[3]), but have you tried just not registering them at all? In theory the class-path scan should pick it up.

<子>1. io.swagger.jaxrs.listing.ApiListingResource
2. io.swagger.jaxrs.listing.SwaggerSerializers
3. 何时使用 JAX-RS 类路径扫描机制

这篇关于Swagger 覆盖路径注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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