Last active
March 12, 2020 15:28
-
-
Save biniama/ba3e0e1df6fd7d320d1bf242de9d469e to your computer and use it in GitHub Desktop.
Swagger Documentation with Spring Boot (working UI)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not sure how this works. I had to use
extends WebMvcConfigurerAdapter
rather thanimplements WebMvcConfigurer