Skip to content

Commit

Permalink
Improve Error Message When Registration Missing
Browse files Browse the repository at this point in the history
Closes gh-15363
  • Loading branch information
jzheaux committed Jul 18, 2024
1 parent caa325f commit dab48d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ private boolean isDispatcherServlet(ServletRegistration registration) {
}
}

private String computeErrorMessage(Collection<? extends ServletRegistration> registrations) {
private static String computeErrorMessage(Collection<? extends ServletRegistration> registrations) {
String template = "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).\n\n"
Expand Down Expand Up @@ -628,7 +628,7 @@ static class DispatcherServletRequestMatcher implements RequestMatcher {
public boolean matches(HttpServletRequest request) {
String name = request.getHttpServletMapping().getServletName();
ServletRegistration registration = this.servletContext.getServletRegistration(name);
Assert.notNull(name, "Failed to find servlet [" + name + "] in the servlet context");
Assert.notNull(registration, computeErrorMessage(this.servletContext.getServletRegistrations().values()));
try {
Class<?> clazz = Class.forName(registration.getClassName());
return DispatcherServlet.class.isAssignableFrom(clazz);
Expand Down Expand Up @@ -670,18 +670,12 @@ RequestMatcher requestMatcher(HttpServletRequest request) {

@Override
public boolean matches(HttpServletRequest request) {
if (this.dispatcherServlet.matches(request)) {
return this.mvc.matches(request);
}
return this.ant.matches(request);
return requestMatcher(request).matches(request);
}

@Override
public MatchResult matcher(HttpServletRequest request) {
if (this.dispatcherServlet.matches(request)) {
return this.mvc.matcher(request);
}
return this.ant.matcher(request);
return requestMatcher(request).matcher(request);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,19 @@ public HttpServletMapping getHttpServletMapping() {
verifyNoMoreInteractions(mvc);
}

@Test
public void matchesWhenNoMappingThenException() {
MockServletContext servletContext = new MockServletContext();
servletContext.addServlet("default", DispatcherServlet.class).addMapping("/");
servletContext.addServlet("path", Servlet.class).addMapping("/services/*");
MvcRequestMatcher mvc = mock(MvcRequestMatcher.class);
AntPathRequestMatcher ant = mock(AntPathRequestMatcher.class);
DispatcherServletDelegatingRequestMatcher requestMatcher = new DispatcherServletDelegatingRequestMatcher(ant,
mvc, servletContext);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/services/endpoint");
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> requestMatcher.matcher(request));
}

private void mockMvcIntrospector(boolean isPresent) {
ApplicationContext context = this.matcherRegistry.getApplicationContext();
given(context.containsBean("mvcHandlerMappingIntrospector")).willReturn(isPresent);
Expand Down

0 comments on commit dab48d2

Please sign in to comment.