@ComponentScan() @EnableAutoConfiguration public class MyApplication extends SpringBootServletInitializer { private static Logger LOG = LoggerFactory.getLogger(MyApplication.class); public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(MyApplication.class, args); DispatcherServlet dispatcherServlet = (DispatcherServlet)ctx.getBean("dispatcherServlet"); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); }
// NEW CODE ABOVE REPLACES THIS! (12/04/15) @Configuration public class MyAppConfig { @Bean // Magic entry public DispatcherServlet dispatcherServlet() { DispatcherServlet ds = new DispatcherServlet(); ds.setThrowExceptionIfNoHandlerFound(true); return ds; } }
DispatcherServlet dispatcherServlet = ...; // might get it from super implementation dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
@Test public void throwExceptionIfNoHandlerFound() throws ServletException, IOException { DispatcherServlet complexDispatcherServlet = new DispatcherServlet(); complexDispatcherServlet.setContextClass(SimpleWebApplicationContext.class); complexDispatcherServlet.setNamespace("test"); complexDispatcherServlet.setThrowExceptionIfNoHandlerFound(true); complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex")); MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertTrue("correct error code", response.getStatus() == HttpServletResponse.SC_NOT_FOUND); }
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return null; } /* ... */ @Override protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) { final DispatcherServlet dispatcherServlet = super.createDispatcherServlet(servletAppContext); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); return dispatcherServlet; } }
@Autowired public void configureDispatcher(DispatcherServlet dispatcherServlet){ dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); }
@Override protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) { DispatcherServlet ds = new DispatcherServlet(servletAppContext); ds.setThrowExceptionIfNoHandlerFound(true); return ds; }
@SpringBootApplication @EnableWebMvc public class Application { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); DispatcherServlet dispatcherServlet = (DispatcherServlet)ctx.getBean("dispatcherServlet"); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); } }
@SpringBootApplication @EnableWebMvc public class Application { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); DispatcherServlet dispatcherServlet = (DispatcherServlet)ctx.getBean("dispatcherServlet"); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); } }
@Bean DispatcherServlet dispatcherServlet () { DispatcherServlet ds = new DispatcherServlet(); ds.setThrowExceptionIfNoHandlerFound(true); return ds; }
@Override public void onStartup(ServletContext servletContext) throws ServletException { //... WebApplicationContext context = getContext(); DispatcherServlet dispatcherServlet = new DispatcherServlet(context); //we did all this to set the below flag dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet",dispatcherServlet ); //.. }
@Configuration @EnableWebMvc @ComponentScan @EnableAutoConfiguration @EnableConfigurationProperties public class FormsRestApplication2{ public static void main(String[] args) throws Exception { ApplicationContext ctx = SpringApplication.run( FormsRestApplication2.class, args); } private static Class<FormsRestApplication2> applicationClass = FormsRestApplication2.class; @Bean public DispatcherServlet dispatcherServlet() { DispatcherServlet ds = new DispatcherServlet(); ds.setThrowExceptionIfNoHandlerFound(true); return ds; } }
public class WebXml implements WebApplicationInitializer{ public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext context = getContext(); servletContext.addListener(new ContextLoaderListener(context)); DispatcherServlet dp = new DispatcherServlet(context); dp.setThrowExceptionIfNoHandlerFound(true); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", dp); dispatcher.setLoadOnStartup(1); dispatcher.addMapping(MAPPING_URL); } }
@Configuration public class AppInitializer implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext rootContext = getWebApplicationContext(); servletContext.addListener(new ContextLoaderListener(rootContext)); servletContext.setInitParameter("defaultHtmlEscape", "true"); } private AnnotationConfigWebApplicationContext getWebApplicationContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("com.app.config"); return context; } @Autowired public void configureDispatcher(DispatcherServlet dispatcherServlet){ dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); } }
@Override public void customize(DispatcherServlet dispatcherServlet) { dispatcherServlet.setDispatchOptionsRequest( this.webMvcProperties.isDispatchOptionsRequest()); dispatcherServlet.setDispatchTraceRequest( this.webMvcProperties.isDispatchTraceRequest()); dispatcherServlet.setThrowExceptionIfNoHandlerFound( this.webMvcProperties.isThrowExceptionIfNoHandlerFound()); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { super.onStartup(servletContext); servletContext.addListener(new SessionListener()); //BEGIN OF NEW CODE WebApplicationContext context = getContext(); DispatcherServlet dispatcherServlet = new DispatcherServlet(context); //we did all this to set the below flag dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet",dispatcherServlet ); //END OF NEW CODE FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter()); encodingFilter.setInitParameter("encoding", "UTF-8"); encodingFilter.setInitParameter("forceEncoding", "true"); encodingFilter.addMappingForUrlPatterns(null, true, "/*"); }
public class WebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { DispatcherServlet dispatcherServlet = new DispatcherServlet(getContext()); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); ServletRegistration.Dynamic registration = container.addServlet("dispatcher", dispatcherServlet); registration.setLoadOnStartup(1); registration.addMapping("/"); } private AnnotationConfigWebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("com.my.config"); context.scan("com.my.controllers"); return context; } }
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
dispatcherServlet.setDispatchOptionsRequest(true); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);