java Spring Boot 摆动API不工作

qzwqbdag  于 2023-02-02  发布在  Java
关注(0)|答案(4)|浏览(156)

下面是我的pom.xml

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>

我使用的是Spring Boot的1.5.3.RELEASE版本,下面是我的swagger配置文件:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket swagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

下面是我的WebSecurityConfig.java

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**");
}

当我从端点http://localhost:8080/v2/api-docs执行get操作时,我会返回JSON:

{
  "swagger": "2.0",
  "info": {
    "description": "Api Documentation",
    "version": "1.0",
    "title": "Api Documentation",
    "termsOfService": "urn:tos",
    "contact": {},
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0"
    }
  },
  "host": "localhost:8080",
  "basePath": "/",
  //ETC
}

但是当我尝试访问localhost:8080/swagger-ui.html的UI时,我得到了一个如下所示的空白页面:

如果我单击该页面,我将通过此

获得晋升
我做错什么了?是不是 Spring 安保问题?

m2xkgtsf

m2xkgtsf1#

您可以使用springfox.documentation.swagger.v2.path属性在应用程序配置中建议Swagger的API描述路径,例如application.yml中的springfox.documentation.swagger.v2.path: /rest/docs
我已经发布了一个on github示例。

06odsfpq

06odsfpq2#

如果您使用版本- V3||国际标准化组织斯普林福克斯〉= 3.0.0

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-boot-starter</artifactId>
   <version>3.0.0</version>
</dependency>

Java代码

@Configuration
@EnableSwagger2

public class SwaggerConfig {

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2).select()
            .apis(RequestHandlerSelectors.basePackage("Your Controller package name"))
            .paths(PathSelectors.any()).build();
}

}
V3浏览器URL -〉http://localhost:8080/swagger-ui/#/运行(必需):制造商清洁

czq61nw1

czq61nw13#

很可能spring-security不允许/阻止您的端点被发现。请尝试将您的ant matchers更改为以下值,看看是否有帮助。security/ui配置端点不正确。

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers(
        "/v2/api-docs", 
        "/swagger-resources/configuration/ui",     
        "/swagger-resources", 
        "/swagger-resources/configuration/security", 
        "/swagger-ui.html",     
        "/webjars/**");
}
mefy6pfw

mefy6pfw4#

将swagger版本更改为2.9.2,它将工作。

<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.9.2</version>
   </dependency>

相关问题