2016-11-25 98 views
0

不用说我知道关于这个主题有很多问题,但我一直在试图解决这个问题2天,现在我阅读的大部分内容都已经过时。Spring Swagger-ui integration

所以啊..这就是我现在所拥有的:

摇篮:

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时,将源设置为上面的链接并在桌面上运行它,它也可以正常工作。不起作用的是把这两样东西放在一起(让它在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工作?

编辑:找到了解决方案

如果有人遇到这种情况,问题是如果你有一个请求参数为@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,因此没有html或css文件可以手动安装到你的webapp中。 –

+0

感谢您的帮助。陷入了同样的问题。 –

回答

0

这就是我们用作设置。一种用于它自己的类@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

感谢您的回复。尝试了这一点,但没有端点的用户界面。 – Rauno

+0

你在找哪个地址?你似乎在那里添加v2,我们的swagger gui位于host: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> 

注释转换以上依赖于摇篮。 版我采用的是2.3.1

package XXXX; 

import springfox.documentation.swagger2.annotations.EnableSwagger2; 

@EnableSwagger2 
public class SwaggerConfiguration { 

} 

我们启用扬鞭配置如上所述。

您可以自定义标题添加文案豆:

@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

感谢您的回复。试过这个,但仍然没有端点的用户界面。 – Rauno

相关问题