Hi,
I wonder if someone already tried to use the new feature in Spring 3.2.0.M2 release that allows to handle exceptions from any controller in a centralized way and return response object instead of view.
It seems to work and my @ControllerAdvice contoller @ExceptionHandler method is invoked when exception is thrown from another controller.
The new feature should also support returning response objects that afterwards should be converted to XML/JSON..., with relevant message convertors, however, I see that spring still return view.
So, is it something that still not completed, even though, the feature looks resolved, or I have some mistake or missing configuration.
Here is my exception handler:
And mvc configuration:Code:@ControllerAdvice public class ExceptionHandlerController { @ExceptionHandler(Exception.class) @ResponseBody public ResponseEntity<ErrorMessage> handleException(Exception ex) { // My object that should be converted to JSON ErrorMessage errorMessage = new ErrorMessage(); errorMessage.set... ResponseEntity<ErrorMessage> responseEntity = new ResponseEntity<ErrorMessage>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR); return responseEntity; } }
Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:security="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <security:global-method-security pre-post-annotations="enabled" > <security:expression-handler ref="expressionHandler" /> </security:global-method-security> <bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler"> <property name="permissionEvaluator" ref="customExpressionHandler"/> </bean> <bean id="customExpressionHandler" class="my.CustomPermissionEvaluator"/> <context:component-scan base-package="my" use-default-filters="false"> <context:include-filter type="regex" expression="\..*\.controller\..*"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <mvc:annotation-driven conversion-service="conversionService" /> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="formatters"> <set> <bean class="my.OidParamAnnotationFormatterFactory"/> <bean class="my.FilterParamAnnotationFormatterFactory"/> </set> </property> </bean> </beans>
Thanks,
Pavel


Reply With Quote
