Example usage for org.springframework.web.servlet DispatcherServlet setThrowExceptionIfNoHandlerFound

List of usage examples for org.springframework.web.servlet DispatcherServlet setThrowExceptionIfNoHandlerFound

Introduction

In this page you can find the example usage for org.springframework.web.servlet DispatcherServlet setThrowExceptionIfNoHandlerFound.

Prototype

public void setThrowExceptionIfNoHandlerFound(boolean throwExceptionIfNoHandlerFound) 

Source Link

Document

Set whether to throw a NoHandlerFoundException when no Handler was found for this request.

Usage

From source file:com.citibanamex.api.locator.atm.CitibanamexApiAtmBranchLocatorApplication.java

/**
 * To handle 404 Not Found Exception// w  w  w . j ava 2  s.c  o m
 * @return
 */
@Bean
DispatcherServlet dispatcherServlet() {
    DispatcherServlet ds = new DispatcherServlet();
    ds.setThrowExceptionIfNoHandlerFound(true);
    return ds;
}

From source file:rashjz.info.com.az.config.MyWebInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.register(SpringWebConfig.class);

    DispatcherServlet dispatcherServlet = new DispatcherServlet(appContext);
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", dispatcherServlet);
    dispatcher.setLoadOnStartup(1);//from  www  . j  a v  a2 s  .co  m
    dispatcher.addMapping("/");

    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceEncoding(true);

    EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
    FilterRegistration.Dynamic characterEncoding = servletContext.addFilter("characterEncoding",
            characterEncodingFilter);
    characterEncoding.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
}

From source file:com.nkapps.billing.configs.WebAppInitializer.java

@Override
public void onStartup(ServletContext container) {
    /*FilterRegistration.Dynamic encodingFilter = container.addFilter("encoding-filter", new CharacterEncodingFilter());
    encodingFilter.setInitParameter("encoding", "UTF-8");
    encodingFilter.setInitParameter("forceEncoding", "true");
    encodingFilter.addMappingForUrlPatterns(null, true, "/*");*/
    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(HibernateConfig.class, ServiceConfig.class, RabbitMQConfig.class);

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

    // Create the dispatcher servlet's Spring application context
    AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
    dispatcherServlet.register(MvcConfig.class);

    DispatcherServlet dp = new DispatcherServlet(dispatcherServlet);
    dp.setThrowExceptionIfNoHandlerFound(true);

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", dp);
    dispatcher.setLoadOnStartup(1);// w  w w  .j  av a2  s .  c  o  m
    dispatcher.addMapping("/");

}

From source file:com.tamnd.app.init.WebMvcInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Create the 'root' Spring application context
    WebApplicationContext context = context();

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

    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcherServlet", dispatcherServlet);
    dispatcher.setLoadOnStartup(1);// w  w  w  .j a  v  a  2 s .c om
    dispatcher.addMapping(MAPPING_URL);

    servletContext
            .addFilter("securityFilter",
                    new DelegatingFilterProxy("springSecurityFilterChain"/*Do not change this name*/))
            .addMappingForUrlPatterns(null, false, "/*");
    //      servletContext.addFilter("hibernateFilter", new OpenSessionInViewFilter())
    //            .addMappingForUrlPatterns(null, false, "/*");
}

From source file:org.smigo.config.WebAppInitializer.java

@Override
protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
    super.beforeSpringSecurityFilterChain(servletContext);
    log.info("Starting servlet context");
    log.info("contextName: " + servletContext.getServletContextName());
    log.info("contextPath:" + servletContext.getContextPath());
    log.info("effectiveMajorVersion:" + servletContext.getEffectiveMajorVersion());
    log.info("effectiveMinorVersion:" + servletContext.getEffectiveMinorVersion());
    log.info("majorVersion:" + servletContext.getMajorVersion());
    log.info("minorVersion:" + servletContext.getMinorVersion());
    log.info("serverInfo:" + servletContext.getServerInfo());
    //               ", virtualServerName:" + servletContext.getVirtualServerName() +
    log.info("toString:" + servletContext.toString());

    for (Enumeration<String> e = servletContext.getAttributeNames(); e.hasMoreElements();) {
        log.info("Attribute:" + e.nextElement());
    }/*w ww  .  jav a 2 s  . c o  m*/
    for (Map.Entry<String, String> env : System.getenv().entrySet()) {
        log.info("System env:" + env.toString());
    }
    for (Map.Entry<Object, Object> prop : System.getProperties().entrySet()) {
        log.info("System prop:" + prop.toString());
    }

    final String profile = System.getProperty("smigoProfile", EnvironmentProfile.PRODUCTION);
    log.info("Starting with profile " + profile);

    WebApplicationContext context = new AnnotationConfigWebApplicationContext() {
        {
            register(WebConfiguration.class);
            setDisplayName("SomeRandomName");
            getEnvironment().setActiveProfiles(profile);
        }
    };

    FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("CharacterEncodingFilter",
            new CharacterEncodingFilter());
    characterEncodingFilter.setInitParameter("encoding", "UTF-8");
    characterEncodingFilter.setInitParameter("forceEncoding", "true");
    characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*");

    servletContext.addListener(new RequestContextListener());
    servletContext.addListener(new ContextLoaderListener(context));

    //http://stackoverflow.com/questions/4811877/share-session-data-between-2-subdomains
    //        servletContext.getSessionCookieConfig().setDomain(getDomain());

    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(false);
    servletContext.addServlet("dispatcher", dispatcherServlet).addMapping("/");
}

From source file:org.statefulj.demo.ddd.config.MVCInitializer.java

@Override
protected void registerDispatcherServlet(ServletContext servletContext) {
    String servletName = getServletName();

    WebApplicationContext servletAppContext = createServletApplicationContext();

    DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext);

    // throw NoHandlerFoundException to Controller when a User requests a non-existent page
    ///*from   ww  w . j  a  v  a  2s .c  o m*/
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

    ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);

    registration.setLoadOnStartup(1);
    registration.addMapping(getServletMappings());
    registration.setAsyncSupported(isAsyncSupported());

    Filter[] filters = getServletFilters();
    if (!ObjectUtils.isEmpty(filters)) {
        for (Filter filter : filters) {
            registerServletFilter(servletContext, filter);
        }
    }

    customizeRegistration(registration);
}