【问题标题】:Swagger api listing is emptySwagger api 列表为空
【发布时间】:2015-09-15 11:07:24
【问题描述】:

最近我在我的一个项目中配置了 swagger。它在 tomcat 上使用 jersey2 和 JAX-WS 来获取宁静的 API。我已经使用以下手册进行配置

https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5

${basepath}/swagger.json 响应如下

{"swagger":"2.0","info":{"version":"1.0.0","title":""},"host":"localhost:8080","basePath":" /myapi","schemes":["http"]}

不幸的是,它不包含我的资源包下的任何 api。 我已经尝试过以下问题的答案

swagger - empty listing with no API

但这也无济于事。 使用 com.wordnik.swagger.* 包的上述答案 但是使用手册我得到了 io.swagger.* 包,它没有 JaxrsApiReader 类

我的假设是 swagger 无法从资源包中扫描我的 api 列表。 但无法弄清楚我错过了哪个配置或哪个代码 sn-p。

有什么帮助吗?....

【问题讨论】:

    标签: api rest jersey jax-rs swagger


    【解决方案1】:

    您似乎忘记用@Api 标记其余端点

    【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我使用了另一种对我有用的方法,即仅在我的 Application 类中添加信息。如果您有一个,这可能会对您有所帮助:

    public class MyApi extends Application {
    
        public MyApi() {
            super();
            BeanConfig beanConfig = new BeanConfig();
            beanConfig.setTitle("MyApi");
            beanConfig.setVersion("0.0.1");
            beanConfig.setSchemes(new String[]{"http", "https"});
            beanConfig.setHost("localhost:8080");
            beanConfig.setBasePath("/mypath");
    
            //putting only the path to my api unblocked me, I removed "io.swagger.resources"
            beanConfig.setResourcePackage("system.organization.api"); 
            beanConfig.setScan(true);
            beanConfig.setPrettyPrint(true);
        }
    
        @Override
        public Set<Class<?>> getClasses() {
            Set<Class<?>> s = new HashSet<>();
    
            s.add(MyApis);
    
            //for swagger
            s.add(ApiListingResource.class);
            s.add(SwaggerSerializers.class);
    
            return s;
        }
    }
    

    然后,带有@API注解的类的链接出现在swagger.json中

    大部分使用您使用的同一手册完成:https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-1.X-Project-Setup-1.5

    【讨论】:

    • 我只需要将@Api 放在服务上。我不需要覆盖 getClasses()。在 web.xml 我添加: resteasy.scantrue
    • 从资源包中删除“io.swagger.resources”对我有用。
    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    相关资源
    最近更新 更多