2017-06-12 97 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