【问题标题】:Jersey JAX-RS, Swagger - getting swagger.json generated but no UIJersey JAX-RS,Swagger - 生成 swagger.json 但没有 UI
【发布时间】:2015-08-06 18:09:20
【问题描述】:

我在加载 Swagger UI/启用 UI 端点时遇到问题。 Maven 项目,Jersey 版本 - 2.12,Swagger 版本 - 1.5.1-M2

我有一个以编程方式配置的球衣网络应用程序。 在我的 ResourceConfig(扩展)中,我为 Swagger 设置了以下内容:

    beanConfig = new BeanConfig();
    beanConfig.setVersion("1.0.0");
    beanConfig.setHost("http:localhost:8080");
    beanConfig.setBasePath("/app/v1");
    beanConfig.setResourcePackage("com.app.features");
    beanConfig.setScan(true);

    注册(beanConfig);

    注册(新的 ApiListingResourceJSON());
    注册(新的 SwaggerSerializers());

我还有一个引导类,我通过 web.xml 加载:

public class Bootstrap extends HttpServlet {

  @Override
  public void init(ServletConfig config) throws ServletException {
    Info info = new Info()
            .title("Swagger Sample App")
            .description("Desc")
            .termsOfService("http://helloreverb.com/terms/")
            .contact(new Contact()
                    .email("apiteam@swagger.io"))
            .license(new License()
                    .name("Apache 2.0")
                    .url("http://www.apache.org/licenses/LICENSE-2.0.html"));

    ServletContext context = config.getServletContext();
    Swagger swagger = new Swagger().info(info);
    context.setAttribute("swagger", swagger);
  }
}

说web.xml:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>Bootstrap</servlet-name>
    <servlet-class>com.app.Bootstrap</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>

我已将 Swagger UI dist 的内容复制到我的网络应用程序文件夹中。

当我在http://localhost:8080/app/v1/swagger.json 上点击 api json 端点时,我确实得到了 json 代码,例如:

{"swagger":"2.0","info":{"version":"1.0.0"},"host":"http:localhost:8080","basePath":"/app/v1"}

但我似乎没有在我期望的路径上看到 Swagger UI(http:localhost:8080/app/v1 或 http:localhost:8080/app/v1/app/v1/index.html)。

很遗憾,我对 Jersey 的感觉不如对 Spring 的感觉舒服,因此欢迎任何帮助。

谢谢

【问题讨论】:

    标签: java rest jersey swagger swagger-ui


    【解决方案1】:

    你需要下载swagger-ui的web项目,放到你的web目录下,并在index.html中配置你的api地址,

    index.html:

      <script type="text/javascript">
        $(function () {
          var url = window.location.search.match(/url=([^&]+)/);
          if (url && url.length > 1) {
            url = decodeURIComponent(url[1]);
          } else {
            url = "http://petstore.swagger.io/v2/swagger.json"; //your api address
          }
          window.swaggerUi = new SwaggerUi({
            url: url,
    

    现在您可以通过访问http://ip:port/path/swagger-ui/index.html 访问swagger-ui

    【讨论】:

    • 我已经这样做了,但没有效果。我认为这不是一个正确暴露的端点。卷曲什么也不返回。只有 path+/swagger.json 和我的 api 端点才会响应。
    • 您的访问路径是什么?我认为你的路径应该是http://localhost:8080/app/v1/app/v1/swagger-ui/index.htmlswagger-ui 文件夹应该存在于index.html 文件夹中。
    • 我们是否可能在谈论不同版本的 swagger-ui?对我来说,webapp 文件夹有新的 Swagger index.html、swagger-ui.js 文件、css 和 fonts 文件夹,你在这里看到:github.com/swagger-api/swagger-ui/tree/master/dist 尝试访问路径:localhost:8080/aopui-service/v1localhost:8080/aopui-service/v1/index.html@987654325 @(这对我来说没有意义)但是没有用。感觉像是端点问题。
    【解决方案2】:

    我们也在 google 组中报道它,但这很可能是因为您的资源不是直接在 com.app.features 包下,而是在诸如 com.app.features.foo 之类的子包下。如果它们跨越多个包,您可以将它们设置为com.app.features.foo,com.app.features.bar

    我相信在1.5.2-M2中深度扫描已经改变了,所以你也可以试试。

    【讨论】:

      【解决方案3】:

      有错别字:

      beanConfig.setHost("http:localhost:8080");
      

      应该是:

      beanConfig.setHost("http://localhost:8080");
      

      如果您使用的是Maven,您能否尝试包含swagger-uidependency:

      <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>swagger-ui</artifactId>
        <version>2.2.10-1</version>
      </dependency>
      

      或更新的版本。

      并尝试在浏览器中打开此网址:

      http://localhost:8080/app/v1/api-docs?url=/app/v1/swagger.json

      【讨论】:

        【解决方案4】:

        请找到以下工作示例

        包 com.vk.test.swagger;

        import static springfox.documentation.builders.PathSelectors.regex;
        
        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configuration;
        
        import springfox.documentation.builders.RequestHandlerSelectors;
        import springfox.documentation.spi.DocumentationType;
        import springfox.documentation.spring.web.plugins.Docket;
        import springfox.documentation.swagger2.annotations.EnableSwagger2;
        
        @Configuration
        @EnableSwagger2
        
        /**
         * 
         * @author vaquar khan
         *
         */
        public class SwaggerConfiguration {
            @Bean
            public Docket productApi() {
                return new Docket(DocumentationType.SWAGGER_2).select()
                        .apis(RequestHandlerSelectors.basePackage("com.vk.test.controller")).paths(regex("/api/apiPath.*"))
                        .build();
        
            }
        
        }
        

        Maven

           <!-- Swagger -->
                <dependency>
                    <groupId>io.springfox</groupId>
                    <artifactId>springfox-swagger2</artifactId>
                    <version>2.6.1</version>
                    <scope>compile</scope>
                </dependency>
                <!-- Swagger UI -->
        
                <dependency>
                    <groupId>io.springfox</groupId>
                    <artifactId>springfox-swagger-ui</artifactId>
                    <version>2.6.1</version>
                    <scope>compile</scope>
                </dependency>
        

        招摇招呼

        http://<servername>:<Port>/swagger-ui.html
        

        【讨论】: