我是Spring的开始,在spring事务方法中无法捕获catch块中的异常
@Path("/codes")
public class CodeFacadeREST extends AbstractFacade<Code> {
@DELETE
@Path("{id}")
@Transactional
public void remove(@PathParam("id") Integer id) throws RestException {
try {
log.info("Removing Entity ::"+id );
super.remove(super.find(id));
} catch (Exception e) {
// control never reach here
log.error("Error ::: " + e.getMessage(), e);
throw new RestException("Error ", e);
}
}
//some other stuff
}
超级班是
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public void remove(T entity) {
log.info("Deleting givne entity: " + entity);
getEntityManager().remove(getEntityManager().merge(entity));
}
}
springconfiguration applicationContext.xml是
<context:annotation-config/>
<context:component-scan base-package="com.esq.rest.service,com.esq.rest.exception" />
<bean class="com.esq.rest.exception.RestExceptionMapper" scope="singleton" />
<!-- REST service beans -->
<bean class="com.esq.rest.service.CodeFacadeREST" />
在运行时我可以在tomcat日志中看到以下异常
错误代码:20000 调用:DELETE FROM DICT.CODE WHERE(ID =?) bind =&gt; [1参数界限] 查询:DeleteObjectQuery(com.vrs.pojo.Code [id = 1]) 在org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:522) 在org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755) 在org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724) 在org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270) 在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 在org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631) 在com.esq.rest.service.CodeFacadeREST $$ EnhancerByCGLIB $$ 9bec2548.remove()
然而同样没有抓住方法尝试catch,我希望这是由于代理。有没有办法解决这个问题。
编辑:
我想通过异常映射器捕获并响应它。
@Provider
public class RestExceptionMapper implements ExceptionMapper<RestException> {
// response user readable form
}