Skip to content

Instantly share code, notes, and snippets.

@biniama
Last active March 12, 2020 15:28
Show Gist options
  • Save biniama/ba3e0e1df6fd7d320d1bf242de9d469e to your computer and use it in GitHub Desktop.
Save biniama/ba3e0e1df6fd7d320d1bf242de9d469e to your computer and use it in GitHub Desktop.
Swagger Documentation with Spring Boot (working UI)
<!-- Spring boot version is 2.0.3.RELEASE-->
<dependencies>
<!-- Swagger 2 Doc Dependency -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger 2 UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
package com.example.springbootswagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerDocConfig implements WebMvcConfigurer {
@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/");
}
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.springbootswagger.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo getApiInfo() {
return new ApiInfoBuilder()
.title("Swagger API Doc")
.description("More description about the API")
.version("1.0.0")
.build();
}
}
@rcollette
Copy link

I'm not sure how this works. I had to use extends WebMvcConfigurerAdapter rather than implements WebMvcConfigurer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment