Skip to content

Commit c20cad4

Browse files
committed
Guarantee compatibility, with spring versions, previous to 5.1
1 parent 35a6fa1 commit c20cad4

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

springdoc-openapi-webmvc-core/src/main/java/org/springdoc/webmvc/api/OpenApiWebMvcResource.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import com.fasterxml.jackson.core.JsonProcessingException;
3232
import io.swagger.v3.oas.annotations.Operation;
3333
import org.apache.commons.lang3.StringUtils;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
3436
import org.springdoc.core.AbstractRequestService;
3537
import org.springdoc.core.ActuatorProvider;
3638
import org.springdoc.core.GenericResponseService;
@@ -63,6 +65,10 @@
6365
@RestController
6466
public class OpenApiWebMvcResource extends OpenApiResource {
6567

68+
/**
69+
* The constant LOGGER.
70+
*/
71+
private static final Logger LOGGER = LoggerFactory.getLogger(OpenApiWebMvcResource.class);
6672
/**
6773
* Instantiates a new Open api web mvc resource.
6874
*
@@ -142,12 +148,16 @@ public String openapiYaml(HttpServletRequest request, @Value(DEFAULT_API_DOCS_UR
142148
@Override
143149
protected String getServerUrl(HttpServletRequest request, String apiDocsUrl) {
144150
String requestUrl = decode(request.getRequestURL().toString());
145-
Map<String, Predicate<Class<?>>> paths = ((RequestMappingHandlerMapping) requestMappingHandlerMapping).getPathPrefixes();
146151
final String[] prefix = { StringUtils.EMPTY };
147-
paths.forEach((path, classPredicate) -> {
148-
if (classPredicate.test(this.getClass()))
149-
prefix[0] = path;
150-
});
152+
try {
153+
Map<String, Predicate<Class<?>>> paths = ((RequestMappingHandlerMapping) requestMappingHandlerMapping).getPathPrefixes();
154+
paths.forEach((path, classPredicate) -> {
155+
if (classPredicate.test(this.getClass()))
156+
prefix[0] = path;
157+
});
158+
} catch (NoSuchMethodError exc){
159+
LOGGER.warn(" Ignoring Path Prefix. Unable to find getPathPrefixes method using requestMappingHandlerMapping.: {}", exc.getMessage());
160+
}
151161
return requestUrl.substring(0, requestUrl.length() - apiDocsUrl.length()- prefix[0].length());
152162
}
153163

0 commit comments

Comments
 (0)