1

Needless to say I know there are a ton of questions about this same subject but I've been stuck trying to fix this for 2 days now and most of what I've read is outdated.

So yeah.. this is what I have right now:

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'
}

Main class:

@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("/");
}

When I visit: http://localhost:8080/v2/api-docs, I get the json documentation just fine. Also when I download swagger-ui from github, set the source to the link above and run it on desktop, it also works fine. What doesn't work is putting these 2 things together (getting it to work at http://localhost:8080/swagger-ui.html).

There are many tutorials such as these, where they claim the stuff above will make swagger-ui work:

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

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

And a ton of other tutorials where they instruct you to add dist folder from swagger-ui git to your project.

Also mapping like this:

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

Fails as well, throwing Scope 'request' is not active for the current thread; exception.

After trying some tutorials from youtube, the ones linked above and many others, all I've seen is "page not found". If anyone could explain what I'm missing I would be very grateful.

TL:DR How do I get swagger-ui.html to work?

EDIT: found the solution.

In case anyone else stumbles upon this, the problem is that if you have a request mapping that takes in a parameter @RequestMapping("/{param}"), dispatcherServlet no longer maps /** to ResourceHttpRequestHandler. The code below fixes that issue.

@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/");
    }
}
Rauno
  • 616
  • 8
  • 22

2 Answers2

0

That's what we use as setup. A class for its own annotated with @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, "firstname.lastname@somewhere.net"),
    null,
    null);

} }

hecko84
  • 1,224
  • 1
  • 16
  • 29
  • Thanks for the reply. Tried this also but there's no endpoint for the ui. – Rauno Nov 25 '16 at 10:19
  • At which address are you looking? you seem to add v2 there, our swagger gui is located at host:8050/swagger-ui.html , where 8050 is the port that is exposed. – hecko84 Nov 25 '16 at 10:23
  • I'm looking at /swagger-ui.html and v2/swagger-ui.html but I see all the endpoints in console (for example I see v2/api-docs just fine) and there's no endpoint for the ui. – Rauno Nov 25 '16 at 10:26
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>

Convert the above dependency to gradle. Version i used is 2.3.1

package XXXX;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
public class SwaggerConfiguration {

}

We enabled the Swagger Config as stated above.

You can add Docket bean for custom headers:

@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));
  }

And finally import the Swagger config in Main Application Class on Spring boot

@Import({SwaggerConfiguration.class})

Rajeev Singla
  • 788
  • 6
  • 7