[Orcid Service | log4j]: SwaggerConfig.java: Updated swagger to add separate folders for libraries.

This commit is contained in:
Konstantina Galouni 2022-10-20 10:31:30 +03:00
parent 25d12c9c2a
commit 189602566d
1 changed files with 97 additions and 86 deletions

View File

@ -1,86 +1,97 @@
package eu.dnetlib.uoaorcidservice.configuration; package eu.dnetlib.uoaorcidservice.configuration;
import eu.dnetlib.uoaorcidservice.configuration.properties.APIProperties; import eu.dnetlib.uoaorcidservice.configuration.properties.APIProperties;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef; import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo; import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter; import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
* Swagger configuration class * Swagger configuration class
*/ */
@Configuration @Configuration
@Profile({"swagger"}) @Profile({"swagger"})
@EnableSwagger2 @EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter { public class SwaggerConfig extends WebMvcConfigurerAdapter {
private final APIProperties apiProperties; private final APIProperties apiProperties;
@Autowired @Autowired
public SwaggerConfig(APIProperties apiProperties) { public SwaggerConfig(APIProperties apiProperties) {
this.apiProperties = apiProperties; this.apiProperties = apiProperties;
} }
@Bean @Bean
public Docket createRestApi() { public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) return new Docket(DocumentationType.SWAGGER_2)
// .globalOperationParameters(globalParameterList()) // .globalOperationParameters(globalParameterList())
.apiInfo(apiInfo()) .apiInfo(apiInfo())
.select() .select()
.apis(RequestHandlerSelectors.any()) .apis(RequestHandlerSelectors.basePackage("eu.dnetlib.uoaorcidservice.controllers"))
.paths(PathSelectors.any()) .paths(PathSelectors.any())
.build(); .build();
} }
private ApiInfo apiInfo() { @Bean
return new ApiInfoBuilder() public Docket createRestApiAuthorizationLibrary() {
.title(this.apiProperties.getTitle()) return new Docket(DocumentationType.SWAGGER_2)
.description(this.apiProperties.getDescription()) .apiInfo(apiInfo())
.version(this.apiProperties.getVersion()) .groupName("Authorization Library")
.build(); .select()
} .apis(RequestHandlerSelectors.basePackage("eu.dnetlib.uoaauthorizationlibrary.controllers"))
.paths(PathSelectors.any())
private List<Parameter> globalParameterList() { .build();
Parameter authTokenHeader = new ParameterBuilder() }
.name("Session") // name of the header
.modelRef(new ModelRef("string")) // data-type of the header private ApiInfo apiInfo() {
.required(false) return new ApiInfoBuilder()
.parameterType("header") .title(this.apiProperties.getTitle())
.description("Session ID") .description(this.apiProperties.getDescription())
.build(); .version(this.apiProperties.getVersion())
return Collections.singletonList(authTokenHeader); .build();
} }
@Override private List<Parameter> globalParameterList() {
public void addViewControllers(ViewControllerRegistry registry) { Parameter authTokenHeader = new ParameterBuilder()
registry.addRedirectViewController("/v2/api-docs", "/v2/api-docs"); .name("Session") // name of the header
registry.addRedirectViewController("/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui"); .modelRef(new ModelRef("string")) // data-type of the header
registry.addRedirectViewController("/swagger-resources/configuration/security", "/swagger-resources/configuration/security"); .required(false)
registry.addRedirectViewController("/swagger-resources", "/swagger-resources"); .parameterType("header")
} .description("Session ID")
.build();
@Override return Collections.singletonList(authTokenHeader);
public void addResourceHandlers(ResourceHandlerRegistry registry) { }
registry.addResourceHandler("/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); @Override
} public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/v2/api-docs", "/v2/api-docs");
} registry.addRedirectViewController("/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui");
registry.addRedirectViewController("/swagger-resources/configuration/security", "/swagger-resources/configuration/security");
registry.addRedirectViewController("/swagger-resources", "/swagger-resources");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}