Java tutorial
package app.api.swagger; import app.AppConfig; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import springfox.documentation.PathProvider; import springfox.documentation.builders.PathSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.*; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spring.web.AbstractPathProvider; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import javax.inject.Inject; import java.util.*; @EnableWebMvc @EnableSwagger2 public class SwaggerConfig extends WebMvcConfigurerAdapter { private static final String AUTH_SCHEMA = "Koid"; private static final String ERROR_MODEL = "Error"; @Inject private AppConfig config; @Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } @Override public void addViewControllers(final ViewControllerRegistry registry) { registry.addViewController("/").setViewName("redirect:swagger-ui.html"); } @Bean public Docket swaggerSpringMvcPlugin() { final Docket docket = new Docket(DocumentationType.SWAGGER_2).select().build(); docket.apiInfo(apiInfo()).pathProvider(pathProvider()); docket.consumes(defaultMediaType()).produces(defaultMediaType()); docket.securitySchemes(securitySchemes()).securityContexts(securityContexts()); docket.globalResponseMessage(RequestMethod.GET, defaultGetResponses()); docket.globalResponseMessage(RequestMethod.PUT, defaultHttpResponses()); docket.globalResponseMessage(RequestMethod.POST, defaultHttpResponses()); docket.globalResponseMessage(RequestMethod.DELETE, defaultHttpResponses()); return docket; } private ApiInfo apiInfo() { return new ApiInfo(config.getSwaggerApiTitle(), config.getSwaggerApiDescription(), config.getSwaggerApiVersion(), null, config.getSwaggerApiContact(), config.getSwaggerApiLicense(), config.getSwaggerApiLicenseUrl()); } private List<ResponseMessage> defaultGetResponses() { final List<ResponseMessage> results = new ArrayList<ResponseMessage>(); results.add(response(HttpStatus.FORBIDDEN, null)); results.add(response(HttpStatus.UNAUTHORIZED, null)); results.add(response(HttpStatus.NOT_FOUND, null)); return results; } private List<ResponseMessage> defaultHttpResponses() { final List<ResponseMessage> results = new ArrayList<ResponseMessage>(); results.add(response(HttpStatus.FORBIDDEN, null)); results.add(response(HttpStatus.UNAUTHORIZED, null)); results.add(response(HttpStatus.BAD_REQUEST, null)); results.add(response(HttpStatus.UNPROCESSABLE_ENTITY, ERROR_MODEL)); return results; } private Set<String> defaultMediaType() { return Collections.singleton(MediaType.APPLICATION_JSON_VALUE); } private PathProvider pathProvider() { return new AbstractPathProvider() { @Override protected String applicationPath() { return config.getSwaggerApiPath(); } @Override protected String getDocumentationPath() { return applicationPath(); } }; } private ResponseMessage response(final HttpStatus status, final String ref) { return new ResponseMessage(status.value(), status.getReasonPhrase(), new ModelRef(ref)); } private List<SecurityContext> securityContexts() { return Arrays.asList(SecurityContext.builder().forPaths(PathSelectors.any()) .securityReferences(securityReferences()).build()); } private SecurityReference securityReference(final String auth) { final AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); final AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; return new SecurityReference(auth, authorizationScopes); } private List<SecurityReference> securityReferences() { return Arrays.asList(securityReference(AUTH_SCHEMA)); } private List<? extends SecurityScheme> securitySchemes() { return Arrays.asList(new BasicAuth(AUTH_SCHEMA)); } }