无法读取swagger JSON

vcirk6k6  于 2022-11-06  发布在  其他
关注(0)|答案(4)|浏览(217)

我已经按照下面的链接为我的REST服务使用Swagger与Spring创建API文档。
http://jakubstas.com/spring-jersey-swagger-configuration/#comment-1726
一切都很顺利,但是当我尝试使用url http://localhost:8080/rest/api-docs访问swagger的api文档时,我得到了无法读取swagger JSON。有人能帮忙吗?

x33g5p2x

x33g5p2x1#

Swagger在本地不起作用!您可以下载Swagger Ui

zu0ti5jz

zu0ti5jz2#

你试试这边。

@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>
k3fezbri

k3fezbri3#

我通过将下一个文件添加到资源文件夹解决了此问题
swagger.properties
添加了属性:
请输入您的浏览器地址。
然后在代码中添加:

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

然后是用于Dockect配置Bean Simple Bean,如:

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

jogvjijk4#

将此问题处理如下:

@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();
    }

相关问题