org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver Java Examples

The following examples show how to use org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver. 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: DelegatingWebMvcConfigurationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void handlerExceptionResolver() throws Exception {

	delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
	delegatingConfig.handlerExceptionResolver();

	verify(webMvcConfigurer).configureMessageConverters(converters.capture());
	verify(webMvcConfigurer).configureContentNegotiation(contentNegotiationConfigurer.capture());
	verify(webMvcConfigurer).configureHandlerExceptionResolvers(exceptionResolvers.capture());

	assertEquals(3, exceptionResolvers.getValue().size());
	assertTrue(exceptionResolvers.getValue().get(0) instanceof ExceptionHandlerExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(1) instanceof ResponseStatusExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(2) instanceof DefaultHandlerExceptionResolver);
	assertTrue(converters.getValue().size() > 0);
}
 
Example #2
Source File: ResponseEntityExceptionHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void supportsAllDefaultHandlerExceptionResolverExceptionTypes() throws Exception {

	Class<ResponseEntityExceptionHandler> clazz = ResponseEntityExceptionHandler.class;
	Method handleExceptionMethod = clazz.getMethod("handleException", Exception.class, WebRequest.class);
	ExceptionHandler annotation = handleExceptionMethod.getAnnotation(ExceptionHandler.class);
	List<Class<?>> exceptionTypes = Arrays.asList(annotation.value());

	for (Method method : DefaultHandlerExceptionResolver.class.getDeclaredMethods()) {
		Class<?>[] paramTypes = method.getParameterTypes();
		if (method.getName().startsWith("handle") && (paramTypes.length == 4)) {
			String name = paramTypes[0].getSimpleName();
			assertTrue("@ExceptionHandler is missing " + name, exceptionTypes.contains(paramTypes[0]));
		}
	}
}
 
Example #3
Source File: DelegatingWebMvcConfigurationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void configureExceptionResolvers() throws Exception {
	List<WebMvcConfigurer> configurers = new ArrayList<>();
	configurers.add(new WebMvcConfigurer() {
		@Override
		public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
			exceptionResolvers.add(new DefaultHandlerExceptionResolver());
		}
	});
	delegatingConfig.setConfigurers(configurers);

	HandlerExceptionResolverComposite composite =
			(HandlerExceptionResolverComposite) delegatingConfig
					.handlerExceptionResolver(delegatingConfig.mvcContentNegotiationManager());
	assertEquals("Only one custom converter is expected", 1, composite.getExceptionResolvers().size());
}
 
Example #4
Source File: WebMvcConfigurationSupport.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * A method available to subclasses for adding default
 * {@link HandlerExceptionResolver HandlerExceptionResolvers}.
 * <p>Adds the following exception resolvers:
 * <ul>
 * <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions through
 * {@link org.springframework.web.bind.annotation.ExceptionHandler} methods.
 * <li>{@link ResponseStatusExceptionResolver} for exceptions annotated with
 * {@link org.springframework.web.bind.annotation.ResponseStatus}.
 * <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring exception types
 * </ul>
 */
protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
	ExceptionHandlerExceptionResolver exceptionHandlerResolver = createExceptionHandlerExceptionResolver();
	exceptionHandlerResolver.setContentNegotiationManager(mvcContentNegotiationManager());
	exceptionHandlerResolver.setMessageConverters(getMessageConverters());
	exceptionHandlerResolver.setCustomArgumentResolvers(getArgumentResolvers());
	exceptionHandlerResolver.setCustomReturnValueHandlers(getReturnValueHandlers());
	if (jackson2Present) {
		exceptionHandlerResolver.setResponseBodyAdvice(
				Collections.singletonList(new JsonViewResponseBodyAdvice()));
	}
	if (this.applicationContext != null) {
		exceptionHandlerResolver.setApplicationContext(this.applicationContext);
	}
	exceptionHandlerResolver.afterPropertiesSet();
	exceptionResolvers.add(exceptionHandlerResolver);

	ResponseStatusExceptionResolver responseStatusResolver = new ResponseStatusExceptionResolver();
	responseStatusResolver.setMessageSource(this.applicationContext);
	exceptionResolvers.add(responseStatusResolver);

	exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}
 
Example #5
Source File: WebMvcConfigurationSupport.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * A method available to subclasses for adding default {@link HandlerExceptionResolver}s.
 * <p>Adds the following exception resolvers:
 * <ul>
 * <li>{@link ExceptionHandlerExceptionResolver}
 * for handling exceptions through @{@link ExceptionHandler} methods.
 * <li>{@link ResponseStatusExceptionResolver}
 * for exceptions annotated with @{@link ResponseStatus}.
 * <li>{@link DefaultHandlerExceptionResolver}
 * for resolving known Spring exception types
 * </ul>
 */
protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
	ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver();
	exceptionHandlerExceptionResolver.setContentNegotiationManager(mvcContentNegotiationManager());
	exceptionHandlerExceptionResolver.setMessageConverters(getMessageConverters());
	if (jackson2Present) {
		List<ResponseBodyAdvice<?>> interceptors = new ArrayList<ResponseBodyAdvice<?>>();
		interceptors.add(new JsonViewResponseBodyAdvice());
		exceptionHandlerExceptionResolver.setResponseBodyAdvice(interceptors);
	}
	exceptionHandlerExceptionResolver.setApplicationContext(this.applicationContext);
	exceptionHandlerExceptionResolver.afterPropertiesSet();
	exceptionResolvers.add(exceptionHandlerExceptionResolver);

	ResponseStatusExceptionResolver responseStatusExceptionResolver = new ResponseStatusExceptionResolver();
	responseStatusExceptionResolver.setMessageSource(this.applicationContext);
	exceptionResolvers.add(responseStatusExceptionResolver);

	exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}
 
Example #6
Source File: WebMvcConfigurationSupport.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * A method available to subclasses for adding default {@link HandlerExceptionResolver}s.
 * <p>Adds the following exception resolvers:
 * <ul>
 * <li>{@link ExceptionHandlerExceptionResolver}
 * for handling exceptions through @{@link ExceptionHandler} methods.
 * <li>{@link ResponseStatusExceptionResolver}
 * for exceptions annotated with @{@link ResponseStatus}.
 * <li>{@link DefaultHandlerExceptionResolver}
 * for resolving known Spring exception types
 * </ul>
 */
protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
	ExceptionHandlerExceptionResolver exceptionHandlerResolver = createExceptionHandlerExceptionResolver();
	exceptionHandlerResolver.setContentNegotiationManager(mvcContentNegotiationManager());
	exceptionHandlerResolver.setMessageConverters(getMessageConverters());
	exceptionHandlerResolver.setCustomArgumentResolvers(getArgumentResolvers());
	exceptionHandlerResolver.setCustomReturnValueHandlers(getReturnValueHandlers());
	if (jackson2Present) {
		exceptionHandlerResolver.setResponseBodyAdvice(
				Collections.<ResponseBodyAdvice<?>>singletonList(new JsonViewResponseBodyAdvice()));
	}
	exceptionHandlerResolver.setApplicationContext(this.applicationContext);
	exceptionHandlerResolver.afterPropertiesSet();
	exceptionResolvers.add(exceptionHandlerResolver);

	ResponseStatusExceptionResolver responseStatusResolver = new ResponseStatusExceptionResolver();
	responseStatusResolver.setMessageSource(this.applicationContext);
	exceptionResolvers.add(responseStatusResolver);

	exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}
 
Example #7
Source File: WebMvcConfigurationSupport.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * A method available to subclasses for adding default
 * {@link HandlerExceptionResolver HandlerExceptionResolvers}.
 * <p>Adds the following exception resolvers:
 * <ul>
 * <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions through
 * {@link org.springframework.web.bind.annotation.ExceptionHandler} methods.
 * <li>{@link ResponseStatusExceptionResolver} for exceptions annotated with
 * {@link org.springframework.web.bind.annotation.ResponseStatus}.
 * <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring exception types
 * </ul>
 */
protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers,
		ContentNegotiationManager mvcContentNegotiationManager) {
	ExceptionHandlerExceptionResolver exceptionHandlerResolver = createExceptionHandlerExceptionResolver();
	exceptionHandlerResolver.setContentNegotiationManager(mvcContentNegotiationManager);
	exceptionHandlerResolver.setMessageConverters(getMessageConverters());
	exceptionHandlerResolver.setCustomArgumentResolvers(getArgumentResolvers());
	exceptionHandlerResolver.setCustomReturnValueHandlers(getReturnValueHandlers());
	if (jackson2Present) {
		exceptionHandlerResolver.setResponseBodyAdvice(
				Collections.singletonList(new JsonViewResponseBodyAdvice()));
	}
	if (this.applicationContext != null) {
		exceptionHandlerResolver.setApplicationContext(this.applicationContext);
	}
	exceptionHandlerResolver.afterPropertiesSet();
	exceptionResolvers.add(exceptionHandlerResolver);

	ResponseStatusExceptionResolver responseStatusResolver = new ResponseStatusExceptionResolver();
	responseStatusResolver.setMessageSource(this.applicationContext);
	exceptionResolvers.add(responseStatusResolver);

	exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}
 
Example #8
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void handlerExceptionResolver() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	HandlerExceptionResolverComposite compositeResolver =
			context.getBean("handlerExceptionResolver", HandlerExceptionResolverComposite.class);

	assertEquals(0, compositeResolver.getOrder());
	List<HandlerExceptionResolver> expectedResolvers = compositeResolver.getExceptionResolvers();

	assertEquals(ExceptionHandlerExceptionResolver.class, expectedResolvers.get(0).getClass());
	assertEquals(ResponseStatusExceptionResolver.class, expectedResolvers.get(1).getClass());
	assertEquals(DefaultHandlerExceptionResolver.class, expectedResolvers.get(2).getClass());

	ExceptionHandlerExceptionResolver eher = (ExceptionHandlerExceptionResolver) expectedResolvers.get(0);
	assertNotNull(eher.getApplicationContext());

	DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(eher);
	List<Object> interceptors = (List<Object>) fieldAccessor.getPropertyValue("responseBodyAdvice");
	assertEquals(1, interceptors.size());
	assertEquals(JsonViewResponseBodyAdvice.class, interceptors.get(0).getClass());

	LocaleContextHolder.setLocale(Locale.ENGLISH);
	try {
		ResponseStatusExceptionResolver rser = (ResponseStatusExceptionResolver) expectedResolvers.get(1);
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
		MockHttpServletResponse response = new MockHttpServletResponse();
		rser.resolveException(request, response, context.getBean(TestController.class), new UserAlreadyExistsException());
		assertEquals("User already exists!", response.getErrorMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example #9
Source File: DelegatingWebMvcConfigurationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void configureExceptionResolvers() throws Exception {
	List<WebMvcConfigurer> configurers = new ArrayList<WebMvcConfigurer>();
	configurers.add(new WebMvcConfigurerAdapter() {
		@Override
		public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
			exceptionResolvers.add(new DefaultHandlerExceptionResolver());
		}
	});
	delegatingConfig.setConfigurers(configurers);

	HandlerExceptionResolverComposite composite =
		(HandlerExceptionResolverComposite) delegatingConfig.handlerExceptionResolver();
	assertEquals("Only one custom converter is expected", 1, composite.getExceptionResolvers().size());
}
 
Example #10
Source File: WebMvcConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void handlerExceptionResolver() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	HandlerExceptionResolverComposite compositeResolver =
			context.getBean("handlerExceptionResolver", HandlerExceptionResolverComposite.class);

	assertEquals(0, compositeResolver.getOrder());
	List<HandlerExceptionResolver> expectedResolvers = compositeResolver.getExceptionResolvers();

	assertEquals(ExceptionHandlerExceptionResolver.class, expectedResolvers.get(0).getClass());
	assertEquals(ResponseStatusExceptionResolver.class, expectedResolvers.get(1).getClass());
	assertEquals(DefaultHandlerExceptionResolver.class, expectedResolvers.get(2).getClass());

	ExceptionHandlerExceptionResolver eher = (ExceptionHandlerExceptionResolver) expectedResolvers.get(0);
	assertNotNull(eher.getApplicationContext());

	DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(eher);
	List<Object> interceptors = (List<Object>) fieldAccessor.getPropertyValue("responseBodyAdvice");
	assertEquals(1, interceptors.size());
	assertEquals(JsonViewResponseBodyAdvice.class, interceptors.get(0).getClass());

	LocaleContextHolder.setLocale(Locale.ENGLISH);
	try {
		ResponseStatusExceptionResolver rser = (ResponseStatusExceptionResolver) expectedResolvers.get(1);
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
		MockHttpServletResponse response = new MockHttpServletResponse();
		rser.resolveException(request, response, context.getBean(TestController.class), new UserAlreadyExistsException());
		assertEquals("User already exists!", response.getErrorMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example #11
Source File: ResponseEntityExceptionHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	this.servletRequest = new MockHttpServletRequest("GET", "/");
	this.servletResponse = new MockHttpServletResponse();
	this.request = new ServletWebRequest(this.servletRequest, this.servletResponse);

	this.exceptionHandlerSupport = new ApplicationExceptionHandler();
	this.defaultExceptionResolver = new DefaultHandlerExceptionResolver();
}
 
Example #12
Source File: DelegatingWebMvcConfigurationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void configureExceptionResolvers() throws Exception {
	List<WebMvcConfigurer> configurers = new ArrayList<>();
	configurers.add(new WebMvcConfigurer() {
		@Override
		public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
			exceptionResolvers.add(new DefaultHandlerExceptionResolver());
		}
	});
	delegatingConfig.setConfigurers(configurers);

	HandlerExceptionResolverComposite composite =
			(HandlerExceptionResolverComposite) delegatingConfig.handlerExceptionResolver();
	assertEquals("Only one custom converter is expected", 1, composite.getExceptionResolvers().size());
}
 
Example #13
Source File: DelegatingWebMvcConfigurationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void handlerExceptionResolver() throws Exception {
	delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
	delegatingConfig.handlerExceptionResolver();

	verify(webMvcConfigurer).configureMessageConverters(converters.capture());
	verify(webMvcConfigurer).configureContentNegotiation(contentNegotiationConfigurer.capture());
	verify(webMvcConfigurer).configureHandlerExceptionResolvers(exceptionResolvers.capture());

	assertEquals(3, exceptionResolvers.getValue().size());
	assertTrue(exceptionResolvers.getValue().get(0) instanceof ExceptionHandlerExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(1) instanceof ResponseStatusExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(2) instanceof DefaultHandlerExceptionResolver);
	assertTrue(converters.getValue().size() > 0);
}
 
Example #14
Source File: AbstractServletHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Initialize a DispatcherServlet instance registering zero or more controller classes
 * and also providing additional bean definitions through a callback.
 */
@SuppressWarnings("serial")
protected WebApplicationContext initServlet(
		final ApplicationContextInitializer<GenericWebApplicationContext> initializer,
		final Class<?>... controllerClasses) throws ServletException {

	final GenericWebApplicationContext wac = new GenericWebApplicationContext();

	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) {
			for (Class<?> clazz : controllerClasses) {
				wac.registerBeanDefinition(clazz.getSimpleName(), new RootBeanDefinition(clazz));
			}

			RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
			mappingDef.getPropertyValues().add("removeSemicolonContent", "false");
			wac.registerBeanDefinition("handlerMapping", mappingDef);
			wac.registerBeanDefinition("handlerAdapter",
					new RootBeanDefinition(RequestMappingHandlerAdapter.class));
			wac.registerBeanDefinition("requestMappingResolver",
					new RootBeanDefinition(ExceptionHandlerExceptionResolver.class));
			wac.registerBeanDefinition("responseStatusResolver",
					new RootBeanDefinition(ResponseStatusExceptionResolver.class));
			wac.registerBeanDefinition("defaultResolver",
					new RootBeanDefinition(DefaultHandlerExceptionResolver.class));

			if (initializer != null) {
				initializer.initialize(wac);
			}

			wac.refresh();
			return wac;
		}
	};

	servlet.init(new MockServletConfig());

	return wac;
}
 
Example #15
Source File: ResponseEntityExceptionHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void supportsAllDefaultHandlerExceptionResolverExceptionTypes() throws Exception {
	Class<ResponseEntityExceptionHandler> clazz = ResponseEntityExceptionHandler.class;
	Method handleExceptionMethod = clazz.getMethod("handleException", Exception.class, WebRequest.class);
	ExceptionHandler annotation = handleExceptionMethod.getAnnotation(ExceptionHandler.class);
	List<Class<?>> exceptionTypes = Arrays.asList(annotation.value());

	for (Method method : DefaultHandlerExceptionResolver.class.getDeclaredMethods()) {
		Class<?>[] paramTypes = method.getParameterTypes();
		if (method.getName().startsWith("handle") && (paramTypes.length == 4)) {
			String name = paramTypes[0].getSimpleName();
			assertTrue("@ExceptionHandler is missing " + name, exceptionTypes.contains(paramTypes[0]));
		}
	}
}
 
Example #16
Source File: ResponseEntityExceptionHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.servletRequest = new MockHttpServletRequest("GET", "/");
	this.servletResponse = new MockHttpServletResponse();
	this.request = new ServletWebRequest(this.servletRequest, this.servletResponse);

	this.exceptionHandlerSupport = new ApplicationExceptionHandler();
	this.defaultExceptionResolver = new DefaultHandlerExceptionResolver();
}
 
Example #17
Source File: DelegatingWebMvcConfigurationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void handlerExceptionResolver() throws Exception {
	delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
	delegatingConfig.handlerExceptionResolver(delegatingConfig.mvcContentNegotiationManager());

	verify(webMvcConfigurer).configureMessageConverters(converters.capture());
	verify(webMvcConfigurer).configureContentNegotiation(contentNegotiationConfigurer.capture());
	verify(webMvcConfigurer).configureHandlerExceptionResolvers(exceptionResolvers.capture());

	assertEquals(3, exceptionResolvers.getValue().size());
	assertTrue(exceptionResolvers.getValue().get(0) instanceof ExceptionHandlerExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(1) instanceof ResponseStatusExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(2) instanceof DefaultHandlerExceptionResolver);
	assertTrue(converters.getValue().size() > 0);
}
 
Example #18
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void handlerExceptionResolver() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	HandlerExceptionResolverComposite compositeResolver =
			context.getBean("handlerExceptionResolver", HandlerExceptionResolverComposite.class);

	assertEquals(0, compositeResolver.getOrder());
	List<HandlerExceptionResolver> expectedResolvers = compositeResolver.getExceptionResolvers();

	assertEquals(ExceptionHandlerExceptionResolver.class, expectedResolvers.get(0).getClass());
	assertEquals(ResponseStatusExceptionResolver.class, expectedResolvers.get(1).getClass());
	assertEquals(DefaultHandlerExceptionResolver.class, expectedResolvers.get(2).getClass());

	ExceptionHandlerExceptionResolver eher = (ExceptionHandlerExceptionResolver) expectedResolvers.get(0);
	assertNotNull(eher.getApplicationContext());

	DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(eher);
	List<Object> interceptors = (List<Object>) fieldAccessor.getPropertyValue("responseBodyAdvice");
	assertEquals(1, interceptors.size());
	assertEquals(JsonViewResponseBodyAdvice.class, interceptors.get(0).getClass());

	LocaleContextHolder.setLocale(Locale.ENGLISH);
	try {
		ResponseStatusExceptionResolver rser = (ResponseStatusExceptionResolver) expectedResolvers.get(1);
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
		MockHttpServletResponse response = new MockHttpServletResponse();
		rser.resolveException(request, response, context.getBean(TestController.class), new UserAlreadyExistsException());
		assertEquals("User already exists!", response.getErrorMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example #19
Source File: AbstractServletHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Initialize a DispatcherServlet instance registering zero or more controller classes
 * and also providing additional bean definitions through a callback.
 */
@SuppressWarnings("serial")
protected WebApplicationContext initServlet(
		final ApplicationContextInitializer<GenericWebApplicationContext> initializer,
		final Class<?>... controllerClasses) throws ServletException {

	final GenericWebApplicationContext wac = new GenericWebApplicationContext();

	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) {
			for (Class<?> clazz : controllerClasses) {
				wac.registerBeanDefinition(clazz.getSimpleName(), new RootBeanDefinition(clazz));
			}

			RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
			mappingDef.getPropertyValues().add("removeSemicolonContent", "false");
			wac.registerBeanDefinition("handlerMapping", mappingDef);
			wac.registerBeanDefinition("handlerAdapter",
					new RootBeanDefinition(RequestMappingHandlerAdapter.class));
			wac.registerBeanDefinition("requestMappingResolver",
					new RootBeanDefinition(ExceptionHandlerExceptionResolver.class));
			wac.registerBeanDefinition("responseStatusResolver",
					new RootBeanDefinition(ResponseStatusExceptionResolver.class));
			wac.registerBeanDefinition("defaultResolver",
					new RootBeanDefinition(DefaultHandlerExceptionResolver.class));

			if (initializer != null) {
				initializer.initialize(wac);
			}

			wac.refresh();
			return wac;
		}
	};

	servlet.init(new MockServletConfig());

	return wac;
}
 
Example #20
Source File: ResponseEntityExceptionHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void supportsAllDefaultHandlerExceptionResolverExceptionTypes() throws Exception {
	Class<ResponseEntityExceptionHandler> clazz = ResponseEntityExceptionHandler.class;
	Method handleExceptionMethod = clazz.getMethod("handleException", Exception.class, WebRequest.class);
	ExceptionHandler annotation = handleExceptionMethod.getAnnotation(ExceptionHandler.class);
	List<Class<?>> exceptionTypes = Arrays.asList(annotation.value());

	for (Method method : DefaultHandlerExceptionResolver.class.getDeclaredMethods()) {
		Class<?>[] paramTypes = method.getParameterTypes();
		if (method.getName().startsWith("handle") && (paramTypes.length == 4)) {
			String name = paramTypes[0].getSimpleName();
			assertTrue("@ExceptionHandler is missing " + name, exceptionTypes.contains(paramTypes[0]));
		}
	}
}
 
Example #21
Source File: ResponseEntityExceptionHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.servletRequest = new MockHttpServletRequest("GET", "/");
	this.servletResponse = new MockHttpServletResponse();
	this.request = new ServletWebRequest(this.servletRequest, this.servletResponse);

	this.exceptionHandlerSupport = new ApplicationExceptionHandler();
	this.defaultExceptionResolver = new DefaultHandlerExceptionResolver();
}
 
Example #22
Source File: ServletWebServerInitializer.java    From spring-fu with Apache License 2.0 4 votes vote down vote up
@Override
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
	exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}
 
Example #23
Source File: AbstractServletHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize a DispatcherServlet instance registering zero or more controller classes
 * and also providing additional bean definitions through a callback.
 */
@SuppressWarnings("serial")
protected WebApplicationContext initServlet(
		final ApplicationContextInitializer<GenericWebApplicationContext> initializer,
		final Class<?>... controllerClasses) throws ServletException {

	final GenericWebApplicationContext wac = new GenericWebApplicationContext();

	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			for (Class<?> clazz : controllerClasses) {
				wac.registerBeanDefinition(clazz.getSimpleName(), new RootBeanDefinition(clazz));
			}

			Class<?> mappingType = RequestMappingHandlerMapping.class;
			RootBeanDefinition beanDef = new RootBeanDefinition(mappingType);
			beanDef.getPropertyValues().add("removeSemicolonContent", "false");
			wac.registerBeanDefinition("handlerMapping", beanDef);

			Class<?> adapterType = RequestMappingHandlerAdapter.class;
			wac.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(adapterType));

			Class<?> resolverType = ExceptionHandlerExceptionResolver.class;
			wac.registerBeanDefinition("requestMappingResolver", new RootBeanDefinition(resolverType));

			resolverType = ResponseStatusExceptionResolver.class;
			wac.registerBeanDefinition("responseStatusResolver", new RootBeanDefinition(resolverType));

			resolverType = DefaultHandlerExceptionResolver.class;
			wac.registerBeanDefinition("defaultResolver", new RootBeanDefinition(resolverType));

			if (initializer != null) {
				initializer.initialize(wac);
			}

			wac.refresh();
			return wac;
		}
	};

	servlet.init(new MockServletConfig());

	return wac;
}