本文介绍了使用Spring MVC无法在服务层中包装DAO异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring MVC处理自定义异常处理。服务层和服务层的DAO层异常处理程序包装该异常,然后由Spring MVC的Controller异常处理程序处理该异常。以下是我的代码:

I am trying to handle custom exception handling using Spring MVC. DAO layer exception handler by service layer and service layer wrap that exception and handle that exception by Controller exception handler by Spring MVC. Following is my code:

@Override
public void saveNewMachineDetails(Machine machine, Configurations configurations) throws DataNotPersist{
logger.info("call service saveNewMachineDetails method");

try{
    machineRepository.saveAndFlush(machine);
}catch(Exception ex){
//  logger.error(ex.getMessage());
    DataNotPersist dataNotPersist =  new DataNotPersist(messageSource.getMessage("code.object.notpersist", null, null),
            messageSource.getMessage("msg.object.notpersist", new Object[]{"Machine"}, null), ex);
    throw dataNotPersist;
}}

在上面的代码中,服务层的DAO层异常处理并将其包装

In the above code the DAO layer exception handle at service layer and wrap that exception in custom exception and throw that exception.

在Web层中,我配置了用于处理异常的异常处理程序,但是当引发异常时,该处理程序不会捕获该异常,我认为因为我无法用自定义异常包装实际异常。以下是我的异常处理程序代码:

In web layer i have configure Exception handler for handling exception, but when the exception is throw, the handler not catch the exception, i think because of i am unable to wrap actual exception with custom exception. Following is my exception handler code:

@ControllerAdvice
public class GlobalExceptionHandler {

private Logger logger =    LoggerFactory.getLogger(GlobalExceptionHandler.class);

@ExceptionHandler(value = DataNotPersist.class)
public ModelAndView defaultExceptionHandler(HttpServletRequest request, DataNotPersist ex)throws Exception {
logger.info("In GlobalExceptionHandler");

logger.debug("********* : "+ex.getMessage());

ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("exception", ex);
modelAndView.addObject("url", request.getRequestURL());
modelAndView.setViewName("common/error");
return modelAndView;
}}


推荐答案

抛出异常,立即回滚事务,并在视图中将我的 Exception 包装到 TransactionException 中。现在,我用 Exception 类处理事务回滚,如下所示:

In service layer when i throw the exception, spring roll back the transaction and wrap my Exception into TransactionException at view. Now i handle the transaction rollback with my Exception class as below:

@Override
@Transactional(value="transaction_manager", rollbackFor={DataNotPersist.class})
public void saveNewMachineDetails(Machine machine, Configurations configurations) throws DataNotPersist{
    logger.info("call service saveNewMachineDetails method");

    try{
        machineRepository.saveAndFlush(machine);
    }catch(DataAccessException ex){
        logger.error(ex.getMessage());
        DataNotPersist dataNotPersist =  new DataNotPersist(messageSource.getMessage("code.object.notpersist", null, null),
                messageSource.getMessage("msg.object.notpersist", new Object[]{"Machine"}, null), ex);
        throw dataNotPersist;
    }}

这篇关于使用Spring MVC无法在服务层中包装DAO异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:20