Java Code Examples for org.springframework.web.servlet.DispatcherServlet#setThrowExceptionIfNoHandlerFound()

The following examples show how to use org.springframework.web.servlet.DispatcherServlet#setThrowExceptionIfNoHandlerFound() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StartHereApplication.java    From java-starthere with MIT License 5 votes vote down vote up
public static void main(String[] args)
{
    checkEnvironmentVariable("OAUTHCLIENTID");
    checkEnvironmentVariable("OAUTHCLIENTSECRET");

    if (!stop)
    {
        ApplicationContext ctx = SpringApplication.run(StartHereApplication.class,
                                                       args);

        DispatcherServlet dispatcherServlet = (DispatcherServlet) ctx.getBean("dispatcherServlet");
        dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    }
}
 
Example 2
Source File: StartHereApplication.java    From java-starthere with MIT License 5 votes vote down vote up
public static void main(String[] args)
{
    checkEnvironmentVariable("OAUTHCLIENTID");
    checkEnvironmentVariable("OAUTHCLIENTSECRET");

    if (!stop)
    {
        ApplicationContext ctx = SpringApplication.run(com.lambdaschool.starthere.StartHereApplication.class, args);

        DispatcherServlet dispatcherServlet = (DispatcherServlet) ctx.getBean("dispatcherServlet");
        dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    }
}
 
Example 3
Source File: WebMvcConfigurer.java    From mysiteforme with Apache License 2.0 5 votes vote down vote up
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
    ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
    registration.setMultipartConfig(multipartConfigElement());
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    return registration;
}
 
Example 4
Source File: MockSpringMvcServlet.java    From karate with MIT License 5 votes vote down vote up
/**
 * Checks if servlet is Dispatcher servlet implementation and then fetches
 * the WebMvcProperties from spring container and configure the dispatcher
 * servlet.
 *
 * @param servlet input servlet implementation
 */
private static void customize(Servlet servlet) {
    if (servlet instanceof DispatcherServlet) {
        DispatcherServlet dispatcherServlet = (DispatcherServlet) servlet;
        WebMvcProperties mvcProperties
                = dispatcherServlet.getWebApplicationContext().getBean(WebMvcProperties.class);
        dispatcherServlet.setThrowExceptionIfNoHandlerFound(mvcProperties.isThrowExceptionIfNoHandlerFound());
        dispatcherServlet.setDispatchOptionsRequest(mvcProperties.isDispatchOptionsRequest());
        dispatcherServlet.setDispatchTraceRequest(mvcProperties.isDispatchTraceRequest());
    }
}
 
Example 5
Source File: SpringMvcJettyComponentTestServer.java    From backstopper with Apache License 2.0 5 votes vote down vote up
private static DispatcherServlet generateDispatcherServlet(WebApplicationContext context) {
    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
    // By setting dispatcherServlet.setThrowExceptionIfNoHandlerFound() to true we get a NoHandlerFoundException
    //      thrown for a 404 instead of being forced to use error pages.
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    return dispatcherServlet;
}
 
Example 6
Source File: Main.java    From backstopper with Apache License 2.0 5 votes vote down vote up
private static DispatcherServlet generateDispatcherServlet(WebApplicationContext context) {
    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
    // By setting dispatcherServlet.setThrowExceptionIfNoHandlerFound() to true we get a NoHandlerFoundException thrown
    //      for a 404 instead of being forced to use error pages. The exception can be directly handled by Backstopper
    //      which is much preferred - you don't lose any context that way.
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    return dispatcherServlet;
}
 
Example 7
Source File: NoHandlerFoundAdviceTraitTest.java    From problem-spring-web with MIT License 4 votes vote down vote up
private void throwExceptionIfNoHandlerFound(final MockMvc mvc) throws NoSuchFieldException, IllegalAccessException {
    final Field field = MockMvc.class.getDeclaredField("servlet");
    field.setAccessible(true);
    final DispatcherServlet servlet = (DispatcherServlet) field.get(mvc);
    servlet.setThrowExceptionIfNoHandlerFound(true);
}
 
Example 8
Source File: MolgenisWebAppInitializer.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
/** A Molgenis common web application initializer */
protected void onStartup(ServletContext servletContext, Class<?> appConfig, int maxFileSize) {
  // Create the 'root' Spring application context
  AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
  rootContext.registerShutdownHook();
  rootContext.setAllowBeanDefinitionOverriding(false);
  rootContext.register(appConfig);

  // Manage the lifecycle of the root application context
  servletContext.addListener(new ContextLoaderListener(rootContext));

  // Register and map the dispatcher servlet
  DispatcherServlet dispatcherServlet = new DispatcherServlet(rootContext);
  dispatcherServlet.setDispatchOptionsRequest(true);
  // instead of throwing a 404 when a handler is not found allow for custom handling
  dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

  ServletRegistration.Dynamic dispatcherServletRegistration =
      servletContext.addServlet("dispatcher", dispatcherServlet);
  if (dispatcherServletRegistration == null) {
    LOG.warn(
        "ServletContext already contains a complete ServletRegistration for servlet 'dispatcher'");
  } else {
    final long maxSize = (long) maxFileSize * MB;
    dispatcherServletRegistration.addMapping("/");
    dispatcherServletRegistration.setMultipartConfig(
        new MultipartConfigElement(null, maxSize, maxSize, FILE_SIZE_THRESHOLD));
    dispatcherServletRegistration.setAsyncSupported(true);
  }

  // Add filters
  Dynamic browserDetectionFiler =
      servletContext.addFilter("browserDetectionFilter", BrowserDetectionFilter.class);
  browserDetectionFiler.setAsyncSupported(true);
  browserDetectionFiler.addMappingForUrlPatterns(
      EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC), false, "*");

  Dynamic etagFilter = servletContext.addFilter("etagFilter", ShallowEtagHeaderFilter.class);
  etagFilter.setAsyncSupported(true);
  etagFilter.addMappingForServletNames(
      EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC), true, "dispatcher");

  // enable use of request scoped beans in FrontController
  servletContext.addListener(new RequestContextListener());

  servletContext.addListener(HttpSessionEventPublisher.class);
}