Skip to content

Commit 409d539

Browse files
author
muriloalvesdev
committed
swagger config
1 parent 7da2f76 commit 409d539

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package br.com.developers.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
8+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
9+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10+
import com.google.common.base.Predicate;
11+
import springfox.documentation.RequestHandler;
12+
import springfox.documentation.builders.ApiInfoBuilder;
13+
import springfox.documentation.builders.PathSelectors;
14+
import springfox.documentation.builders.RequestHandlerSelectors;
15+
import springfox.documentation.service.ApiInfo;
16+
import springfox.documentation.service.Contact;
17+
import springfox.documentation.spi.DocumentationType;
18+
import springfox.documentation.spring.web.plugins.Docket;
19+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
20+
21+
@Configuration
22+
@EnableSwagger2
23+
@EnableWebMvc
24+
public class SwaggerConfig extends WebMvcConfigurerAdapter {
25+
26+
@Value("${developer.name}")
27+
private String name;
28+
29+
@Value("${developer.linkedin}")
30+
private String linkedin;
31+
32+
@Value("${developer.email}")
33+
private String email;
34+
35+
@Value("${title.swagger}")
36+
private String title;
37+
38+
@Value("${description.swagger}")
39+
private String description;
40+
41+
@Value("${license.swagger}")
42+
private String license;
43+
44+
@Value("${license.url.swagger}")
45+
private String licenseUrl;
46+
47+
@Value("${version.software}")
48+
private String version;
49+
50+
@Bean
51+
public Docket api() {
52+
return new Docket(DocumentationType.SWAGGER_2).select().apis(apis()).paths(PathSelectors.any())
53+
.build().apiInfo(apiInfo());
54+
}
55+
56+
@Override
57+
public void addViewControllers(ViewControllerRegistry registry) {
58+
registry.addRedirectViewController("v2/api-docs", "/v2/api-docs");
59+
registry.addRedirectViewController("swagger-resources/configuration/ui",
60+
"/swagger-resources/configuration/ui");
61+
registry.addRedirectViewController("swagger-resources/configuration/security",
62+
"/swagger-resources/configuration/security");
63+
registry.addRedirectViewController("swagger-resources", "/swagger-resources");
64+
}
65+
66+
@Override
67+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
68+
registry.addResourceHandler("swagger-ui.html**")
69+
.addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
70+
registry.addResourceHandler("webjars/**")
71+
.addResourceLocations("classpath:/META-INF/resources/webjars/");
72+
}
73+
74+
private Predicate<RequestHandler> apis() {
75+
return RequestHandlerSelectors.basePackage("br.com.developers");
76+
}
77+
78+
private ApiInfo apiInfo() {
79+
return new ApiInfoBuilder().contact(new Contact(this.name, this.linkedin, this.email))
80+
.description(this.description).license(this.license).licenseUrl(this.licenseUrl)
81+
.version(this.version).title(this.title).build();
82+
}
83+
84+
}

0 commit comments

Comments
 (0)