下面列出了org.springframework.web.servlet.DispatcherServlet#setThrowExceptionIfNoHandlerFound ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
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);
}
}
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
registration.setMultipartConfig(multipartConfigElement());
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
return registration;
}
/**
* 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());
}
}
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;
}
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;
}
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);
}
/** 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);
}