2016-11-25 22 views
0

言うまでもなく、この同じ主題についてたくさんの質問がありますが、私はこれを2日間修正しようとしていましたが、読んだことのほとんどは時代遅れです。Spring Swagger-ui integration

はそうそう...これは私が今持っているものです。

のGradle:

dependencies { 
compile 'org.springframework.cloud:spring-cloud-starter-hystrix' 
compile 'org.springframework.boot:spring-boot-starter-web' 
compile 'org.springframework.boot:spring-boot-starter-actuator' 
compile 'org.springframework.boot:spring-boot-starter-security' 
compile 'org.springframework.ws:spring-ws-core' 
compile 'io.springfox:springfox-swagger2:2.4.0' 
compile 'io.springfox:springfox-swagger-ui:2.4.0' 
} 

メインクラス:

@EnableSwagger2 
public class Application { 
public static void main(String[] args) { 
     SpringApplication.run(FeedServiceApplication.class, args); 
} 

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

私が訪問:http://localhost:8080/v2/api-docs、私はJSON形式のドキュメントを取得しますちょうど良い。また、githubからswagger-uiをダウンロードして、ソースを上記のリンクに設定してデスクトップ上で実行すると、うまく動作します。動作しないものは、これら2つのものをまとめて(http://localhost:8080/swagger-ui.htmlで動作させる)ものです。

彼らは上記のものが闊歩-UIの仕事になりますと主張、これらのような多くのチュートリアル、があります。

http://kubecloud.io/guide-using-swagger-for-documenting-your-spring-boot-rest-api/

http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

そして彼らがあなたを指示する他のチュートリアルのトンは、 swagger-ui gitのdistフォルダをプロジェクトに追加します。

はまた、このようなマッピング:

@Configuration 
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter { 

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("**/swagger-ui.html") 
      .addResourceLocations("classpath:/META-INF/resources/"); 
    registry.addResourceHandler("**/webjars/**") 
      .addResourceLocations("classpath:/META-INF/resources/webjars/"); 
    } 
} 

スコープ「リクエスト」を投げることは、現在のスレッドのためにアクティブでない、同様に失敗します。例外。

YouTubeからいくつかのチュートリアルを試した後、上にリンクされたチュートリアルや他の多くのチュートリアルは、「ページが見つかりません」と表示されています。誰かが私が逃していることを説明できるなら、私は非常に感謝します。

TL:DR swagger-ui.htmlを動作させるにはどうすればよいですか?

EDIT:ソリューションを見つけました。

誰かがこの問題を抱えている場合は、パラメータ@RequestMapping("/{param}")を使用するリクエストマッピングがあると、dispatcherServletは/ **をResourceHttpRequestHandlerにマップしなくなります。以下のコードはその問題を修正しています。

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

    @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("swagger-ui.html") 
     .addResourceLocations("classpath:/META-INF/resources/"); 

     registry.addResourceHandler("/webjars/**") 
     .addResourceLocations("classpath:/META-INF/resources/webjars/"); 
    } 
} 
+0

swagger-uiはspringfoxが提供するwebjarを使用する必要がありますので、webappに手動でインストールするhtmlまたはcssファイルはありません。 –

+0

助けてくれてありがとう。同じ問題に悩まされました。 –

答えて

0

これはセットアップとして使用するものです。独自のクラスは、上記の依存関係のGradleに変換@Configuration

@Configuration 
@EnableSwagger2 
public class SwaggerConfig { 
    @Bean 
    public Docket api() { 
    return new Docket(DocumentationType.SWAGGER_2) 
     .select() 
     .apis(RequestHandlerSelectors.any()) 
     .paths(Predicates.not(PathSelectors.regex("/error"))) 
     .build() 
     .apiInfo(apiInfo()); 
    } 
    private ApiInfo apiInfo() { 
    return new ApiInfo(
     "APP NAME", 
     "APP DESCRIPTION", 
     "1.0", 
     null, 
     new Contact("Firstname Lastname", null, "[email protected]"), 
    null, 
    null); 

}}

+0

返事をありがとう。これも試しましたが、UIのエンドポイントはありません。 – Rauno

+0

お探しの住所は? v2を追加したようですが、swagger guiはホスト:8050/swagger-ui.htmlにあります.8050は公開されているポートです。 – hecko84

+0

私は/swagger-ui.htmlとv2/swagger-ui.htmlを見ていますが、コンソールのすべてのエンドポイントが表示されています(たとえば、v2/api-docsはうまく表示されています)、uiのエンドポイントはありません。 – Rauno

0
<dependency> 
     <groupId>io.springfox</groupId> 
     <artifactId>springfox-swagger2</artifactId> 
     <version>${swagger.version}</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> 
    <dependency> 
     <groupId>io.springfox</groupId> 
     <artifactId>springfox-swagger-ui</artifactId> 
     <version>${swagger.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>io.springfox</groupId> 
     <artifactId>springfox-spi</artifactId> 
     <version>${swagger.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>io.springfox</groupId> 
     <artifactId>springfox-core</artifactId> 
     <version>${swagger.version}</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-spring-web --> 
    <dependency> 
     <groupId>io.springfox</groupId> 
     <artifactId>springfox-spring-web</artifactId> 
     <version>${swagger.version}</version> 
    </dependency> 

で注釈します。私が使用し バージョンは、上述のように私たちが闊歩Config]を有効に2.3.1

package XXXX; 

import springfox.documentation.swagger2.annotations.EnableSwagger2; 

@EnableSwagger2 
public class SwaggerConfiguration { 

} 

です。

カスタムヘッダーのドケットBeanを追加することができます。

@Bean 
    public Docket docket() { 

Parameter parameterAuthorization = 
     new ParameterBuilder().name("Authorization").description("Authentication of the API User") 
      .modelRef(new ModelRef("string")).parameterType("header").required(true).build(); 

    Parameter parameterClientUserId = 
     new ParameterBuilder().name("user_id").description("Client user identifier") 
      .modelRef(new ModelRef("string")).parameterType("header").required(true).build(); 

    return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()) 
     .paths(PathSelectors.any()).build() 
     .globalOperationParameters(Lists.newArrayList(parameterClientUserId, parameterAuthorization)); 
    } 

そして最後に

春ブーツ

@import({SwaggerConfiguration.class})のメインアプリケーションクラスで闊歩の設定をインポートします

+0

返事をありがとう。これを試しましたが、まだUiのエンドポイントはありません。 – Rauno

関連する問題