【问题标题】:Can't read swagger JSON无法读取招摇的 JSON
【发布时间】:2015-02-26 21:02:41
【问题描述】:

我按照下面的链接使用 Swagger 和 Spring 为我的 REST 服务创建 API 文档。

http://jakubstas.com/spring-jersey-swagger-configuration/#comment-1726

一切都很顺利,但是当我尝试使用 url http://localhost:8080/rest/api-docs 访问 api 文档以获取 swagger 时,我得到 无法读取 swagger JSON。有人可以帮忙吗?

【问题讨论】:

  • 您的 /rest/api-docs 路径应该包含一个 swagger 规范文件。当您的浏览器指向该 url 并且如果您安装了swagger-ui,它将从此文件中读取并将其呈现到一个漂亮的 API 文档网页。

标签: spring rest swagger swagger-ui


【解决方案1】:

Swagger 不适用于本地! 本地可以下载Swagger Ui

【讨论】:

  • 您能否提供更多详细信息?为什么说本地不行?
  • 如果你在'localhost'中启动你的应用程序你不能使用在线版本。您需要为本地下载 Swagger UI 并运行它。
【解决方案2】:

你试试这个方法。

@Configuration
@EnableSwagger
// Loads the spring beans required by the framework
public class MySwaggerConfig
{

    private SpringSwaggerConfig springSwaggerConfig;

    /**
     * Required to autowire SpringSwaggerConfig
     */
    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
    {
        this.springSwaggerConfig = springSwaggerConfig;
    }

    /**
     * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
     * framework - allowing for multiple swagger groups i.e. same code base
     * multiple swagger resource listings.
     */
    @Bean
    public SwaggerSpringMvcPlugin customImplementation()
    {
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(
                ".*?");
    }

    private ApiInfo apiInfo()
    {
        ApiInfo apiInfo = new ApiInfo(
                "xx", 
                "xxxx",
                "My Apps API terms of service", 
                "xxx",
                null,
                null);
        return apiInfo;
    }
}







 <dependency>
                <groupId>com.mangofactory</groupId>
                <artifactId>swagger-springmvc</artifactId>
                <version>0.9.5</version>
            </dependency>

【讨论】:

    【解决方案3】:

    我通过将下一个文件添加到资源文件夹解决了这个问题

    swagger.properties

    在那里添加了属性:

    springfox.documentation.swagger.v2.path=/api/swagger.json

    然后在代码中添加:

    @Configuration
    @EnableSwagger2
    @PropertySource(value = "classpath:swagger.properties")
    public class PathConfiguration {
    @Value("${springfox.documentation.swagger.v2.path}")
    private String swagger2Endpoint;
    

    然后是 Dockect 配置的 bean 简单 bean,例如:

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
    

    【讨论】:

      【解决方案4】:

      它会使这个问题如下:

      @Bean
      public Docket createRestApi() {
          return new Docket(DocumentationType.SWAGGER_2)
                  .apiInfo(apiInfo())
                  .groupName("Swagger Group One API")
                  .select()
                  .apis(RequestHandlerSelectors.basePackage("com.xingyun"))
                  .paths(PathSelectors.any())
                  .build();
      }
      

      固定如下就可以了

      @Bean
          public Docket createRestApi() {
              return new Docket(DocumentationType.SWAGGER_2)
                      .apiInfo(apiInfo())
                      .groupName("SwaggerGroupOneAPI")
                      .select()
                      .apis(RequestHandlerSelectors.basePackage("com.xingyun"))
                      .paths(PathSelectors.any())
                      .build();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-10
        • 1970-01-01
        相关资源
        最近更新 更多