|
18 | 18 |
|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.Arrays;
|
| 21 | +import java.util.Collection; |
21 | 22 | import java.util.LinkedHashMap;
|
22 | 23 | import java.util.List;
|
23 | 24 | import java.util.Map;
|
@@ -320,14 +321,17 @@ public C requestMatchers(HttpMethod method, String... patterns) {
|
320 | 321 | if (!hasDispatcherServlet(registrations)) {
|
321 | 322 | return requestMatchers(RequestMatchers.antMatchersAsArray(method, patterns));
|
322 | 323 | }
|
323 |
| - Assert.isTrue(registrations.size() == 1, |
324 |
| - "This method cannot decide whether these patterns are Spring MVC patterns or not. If this endpoint is a Spring MVC endpoint, please use requestMatchers(MvcRequestMatcher); otherwise, please use requestMatchers(AntPathRequestMatcher)."); |
| 324 | + if (registrations.size() > 1) { |
| 325 | + String errorMessage = computeErrorMessage(registrations.values()); |
| 326 | + throw new IllegalArgumentException(errorMessage); |
| 327 | + } |
325 | 328 | return requestMatchers(createMvcMatchers(method, patterns).toArray(new RequestMatcher[0]));
|
326 | 329 | }
|
327 | 330 |
|
328 | 331 | private Map<String, ? extends ServletRegistration> mappableServletRegistrations(ServletContext servletContext) {
|
329 | 332 | Map<String, ServletRegistration> mappable = new LinkedHashMap<>();
|
330 |
| - for (Map.Entry<String, ? extends ServletRegistration> entry : servletContext.getServletRegistrations().entrySet()) { |
| 333 | + for (Map.Entry<String, ? extends ServletRegistration> entry : servletContext.getServletRegistrations() |
| 334 | + .entrySet()) { |
331 | 335 | if (!entry.getValue().getMappings().isEmpty()) {
|
332 | 336 | mappable.put(entry.getKey(), entry.getValue());
|
333 | 337 | }
|
@@ -355,6 +359,19 @@ private boolean hasDispatcherServlet(Map<String, ? extends ServletRegistration>
|
355 | 359 | return false;
|
356 | 360 | }
|
357 | 361 |
|
| 362 | + private String computeErrorMessage(Collection<? extends ServletRegistration> registrations) { |
| 363 | + String template = "This method cannot decide whether these patterns are Spring MVC patterns or not. " |
| 364 | + + "If this endpoint is a Spring MVC endpoint, please use requestMatchers(MvcRequestMatcher); " |
| 365 | + + "otherwise, please use requestMatchers(AntPathRequestMatcher).\n\n" |
| 366 | + + "This is because there is more than one mappable servlet in your servlet context: %s.\n\n" |
| 367 | + + "For each MvcRequestMatcher, call MvcRequestMatcher#setServletPath to indicate the servlet path."; |
| 368 | + Map<String, Collection<String>> mappings = new LinkedHashMap<>(); |
| 369 | + for (ServletRegistration registration : registrations) { |
| 370 | + mappings.put(registration.getClassName(), registration.getMappings()); |
| 371 | + } |
| 372 | + return String.format(template, mappings); |
| 373 | + } |
| 374 | + |
358 | 375 | /**
|
359 | 376 | * <p>
|
360 | 377 | * If the {@link HandlerMappingIntrospector} is available in the classpath, maps to an
|
|
0 commit comments