Skip to content

Commit 9b7ef11

Browse files
author
springdoc
committed
Fixes Wrong redirect with modified spring.mvc.servlet.path #238
1 parent 649344f commit 9b7ef11

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public final class Constants {
2222
public static final String APPLICATION_OPENAPI_YAML = "application/vnd.oai.openapi";
2323
public static final String DEFAULT_SWAGGER_UI_PATH = DEFAULT_PATH_SEPARATOR + "swagger-ui.html";
2424
public static final String SWAGGER_UI_PATH = "${springdoc.swagger-ui.path:#{T(org.springdoc.core.Constants).DEFAULT_SWAGGER_UI_PATH}}";
25+
public static final String MVC_SERVLET_PATH ="${spring.mvc.servlet.path:#{null}}";
2526
public static final String GET_METHOD = "get";
2627
public static final String POST_METHOD = "post";
2728
public static final String PUT_METHOD = "put";

springdoc-openapi-ui/src/main/java/org/springdoc/ui/SwaggerWelcome.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.springdoc.ui;
22

33
import io.swagger.v3.oas.annotations.Operation;
4+
import org.apache.commons.lang3.StringUtils;
45
import org.springdoc.core.SwaggerUiConfigProperties;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.beans.factory.annotation.Value;
@@ -26,19 +27,26 @@ class SwaggerWelcome {
2627
@Value(SWAGGER_UI_PATH)
2728
private String swaggerPath;
2829

30+
@Value(MVC_SERVLET_PATH)
31+
private String mvcServletPath;
32+
2933
@Autowired
3034
private SwaggerUiConfigProperties swaggerUiConfig;
3135

3236
@Operation(hidden = true)
3337
@GetMapping(SWAGGER_UI_PATH)
3438
public String redirectToUi(HttpServletRequest request) {
3539
String contextPath = request.getContextPath();
40+
if (StringUtils.isNotBlank(mvcServletPath))
41+
contextPath += mvcServletPath;
3642
String uiRootPath = "";
3743
if (swaggerPath.contains("/")) {
3844
uiRootPath = swaggerPath.substring(0, swaggerPath.lastIndexOf('/'));
3945
}
4046
StringBuilder sbUrl = new StringBuilder();
4147
sbUrl.append(REDIRECT_URL_PREFIX);
48+
if (StringUtils.isNotBlank(mvcServletPath))
49+
sbUrl.append(mvcServletPath);
4250
sbUrl.append(uiRootPath);
4351
sbUrl.append(SWAGGER_UI_URL);
4452
if (contextPath.endsWith(DEFAULT_PATH_SEPARATOR)) {

0 commit comments

Comments
 (0)