2017-06-12 93 views
0

这里是我的pom.xml春天引导扬鞭API不工作

<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启动的版本1.5.3.RELEASE。这是我招摇的配置文件:

@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做一个得到我让我的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我得到一个空白页面,看起来像这样: enter image description here

如果我点击页面上,我获得晋升与此 enter image description here

我在做什么错?这是春季安全问题吗?

+0

你可以把你的代码放在什么地方吗? – chenrui

+0

尝试将方法名称从'swagger()'更改为'SwaggerConfig'类中的'api()'。 –

+0

尝试使用url:http:// localhost:8080/swagger-ui.html#/,并在末尾加上**#**符号 –

回答

3

您可以使用springfox.documentation.swagger.v2.path属性(例如,)来向应用程序配置中的Swagger建议API描述路径。 springfox.documentation.swagger.v2.path: /rest/docs in application.yml

我已经发布了一个示例on github

+0

这并没有真正的帮助,因为我可以从'http:// localhost:8080/v2/api-docs'访问api json。我只是无法从swagger-ui.html中读取 – Richard

+0

@Richard尝试在忽略列表中将''/ swagger-resources''更改为''/ swagger-resources/**“ Swagger进入'/ swagger-resources/configuration/ui'端点来获得它的配置。 – jihor

+0

@Richard我用安全配置更新了我的github示例。 – jihor

0

它极有可能的弹簧安全性不允许/阻止您的端点被发现。尝试改变你的蚂蚁匹配到以下,看看是否有帮助。安全/用户配置端点不正确。

@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/**"); 
} 
+0

我认为这也是一个春季安全问题,但这不幸的是没有奏效 – Richard