Skip to content

Commit d27e699

Browse files
Upgrade swagger from version 2 to 3; (#496)
Alter related dependency libs and configurations; Disable default swagger page with sample data;
1 parent b75333b commit d27e699

File tree

4 files changed

+25
-39
lines changed

4 files changed

+25
-39
lines changed

center/build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ dependencies {
5252

5353
compile group: 'org.springframework.security', name: 'spring-security-oauth2-client', version: '5.2.2.RELEASE'
5454

55-
compile('io.springfox:springfox-swagger2:3.0.0')
56-
compile('io.springfox:springfox-swagger-ui:3.0.0')
57-
compile('io.springfox:springfox-boot-starter:3.0.0')
55+
compile('org.springdoc:springdoc-openapi-core:1.1.49')
56+
compile('org.springdoc:springdoc-openapi-ui:1.4.1')
5857

5958
compile group: 'org.jsoup', name: 'jsoup', version: '1.10.1'
6059
compile group: 'org.yaml', name: 'snakeyaml', version: '1.25'

center/src/main/java/com/microsoft/hydralab/center/config/SwaggerConfig.java

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,19 @@
33

44
package com.microsoft.hydralab.center.config;
55

6-
import org.springframework.beans.factory.annotation.Value;
7-
import org.springframework.context.annotation.Bean;
6+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
7+
import io.swagger.v3.oas.annotations.info.Contact;
8+
import io.swagger.v3.oas.annotations.info.Info;
89
import org.springframework.context.annotation.Configuration;
9-
import springfox.documentation.builders.ApiInfoBuilder;
10-
import springfox.documentation.builders.PathSelectors;
11-
import springfox.documentation.builders.RequestHandlerSelectors;
12-
import springfox.documentation.service.ApiInfo;
13-
import springfox.documentation.service.Contact;
14-
import springfox.documentation.spi.DocumentationType;
15-
import springfox.documentation.spring.web.plugins.Docket;
16-
import springfox.documentation.swagger2.annotations.EnableSwagger2;
17-
18-
import java.net.InetAddress;
19-
import java.net.UnknownHostException;
2010

2111
/**
2212
* Created by bsp on 17/12/16.
2313
*/
2414
@Configuration
25-
@EnableSwagger2
26-
//@Profile({"dev", "test"})
15+
@OpenAPIDefinition(info =
16+
@Info(title = "Hydra Lab API", version = "1.0.0", description = "Hydra Lab", contact =
17+
@Contact(name = "Microsoft Hydra Lab Support", email = "hydra_lab_support@microsoft.com")
18+
)
19+
)
2720
public class SwaggerConfig {
28-
@Value("${server.port}")
29-
private String port;
30-
31-
@Bean
32-
public Docket docket() throws UnknownHostException {
33-
ApiInfo apiInfo = new ApiInfoBuilder()
34-
.title("Hydra Lab API")
35-
.description("Hydra Lab")
36-
.termsOfServiceUrl("")
37-
.contact(new Contact("Microsoft Hydra Lab Support ", "", "hydra_lab_support@microsoft.com"))
38-
.version("3.0.0")
39-
.build();
40-
41-
return new Docket(DocumentationType.SWAGGER_2)
42-
.apiInfo(apiInfo)
43-
.host(String.format("%s:%s", InetAddress.getLocalHost().getHostName(), port))
44-
.select()
45-
.apis(RequestHandlerSelectors.basePackage("com.microsoft.hydralab.center.controller"))
46-
.paths(PathSelectors.any())
47-
.build();
48-
}
4921
}

center/src/main/java/com/microsoft/hydralab/center/config/WebMvcConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
import com.microsoft.hydralab.center.interceptor.BaseInterceptor;
88
import com.microsoft.hydralab.center.interceptor.CorsInterceptor;
99
import com.microsoft.hydralab.common.util.Const;
10+
import org.springframework.context.annotation.Bean;
1011
import org.springframework.context.annotation.Configuration;
1112
import org.springframework.http.converter.HttpMessageConverter;
13+
import org.springframework.http.converter.StringHttpMessageConverter;
1214
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
1315
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
1416
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
1517
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
18+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
1619

1720
import javax.annotation.Resource;
1821
import java.util.List;
@@ -55,6 +58,14 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
5558

5659
@Override
5760
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
61+
// Sequence should be kept, currently this converter is only used for /v3/api-docs endpoint to avoid malformed content which should be in json format
62+
converters.add(new StringHttpMessageConverter());
5863
converters.add(fastJsonHttpMessageConverter);
5964
}
65+
66+
@Bean
67+
public InternalResourceViewResolver defaultViewResolver() {
68+
// Add this to make sure redirect can be made when /swagger-ui.html would redirect to swagger-ui/index.html using the configUrl in /v3/api-docs
69+
return new InternalResourceViewResolver();
70+
}
6071
}

center/src/main/resources/application.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ logging:
4646
level:
4747
org.hibernate.SQL: INFO
4848

49+
springdoc:
50+
swagger-ui:
51+
disable-swagger-default-url: true
52+
4953
app:
5054
batteryStrategy: ${BATTERY_STRATEGY:Economic}
5155
access-token-limit: 2

0 commit comments

Comments
 (0)