http 500和重复键值违反了唯一约束 - spring mvc和hibernate

时间:2014-10-05 21:45:32

标签: hibernate spring-mvc

我正在尝试通过@ControllerAdvice学习如何处理Spring MVC和Hibernate异常。在我的用户表中,我有唯一的名称和电子邮件列。所以我想在用户违反唯一约束时显示近似消息:

这是我的控制者:

@RequestMapping(value = "/register", method=RequestMethod.POST)
public String submitRegisterForm(
        @RequestParam("userName") final String username, 
        @RequestParam("email") final String email,
        @RequestParam("password") final String password, Model model)
{
    String violatedContrainst = "Error to create a new account";
    Customer aCustomer = registerManager.registerCustomer(username, email, password);
        if(aCustomer != null) {
             LOGGER.info("Success register a new Account");
             return "/home";
         } else {
             LOGGER.info(violatedContrainst);
             throw new CustomerGenericException("E888", violatedContrainst);
         }
    }
}

My GlobalHandler:

@ControllerAdvice
public class GlobalExceptionController {
    private final static Logger LOGGER =
                    Logger.getLogger(GlobalExceptionController.class.getName());

    @ExceptionHandler(CustomerGenericException.class)
    public ModelAndView handleRegisterException(CustomerGenericException ex) {
        LOGGER.info("Register Hanler");
        ModelAndView model = new ModelAndView("/register");
        model.addObject("errMsg", ex.getErrMsg());
        return model;
    }
}

我的CustomerGenericException:

public class CustomerGenericException extends RuntimeException {

    private static final long serialVersionUID = -3471542841610381393L;

    private String errCode;
    private String errMsg;

    public String getErrCode() {    return errCode; }
    public void setErrCode(String errCode) {    this.errCode = errCode; }

    public String getErrMsg() { return errMsg;  }
    public void setErrMsg(String errMsg) {  this.errMsg = errMsg;   }

    public CustomerGenericException(String errCode, String errMsg) {
        this.errCode = errCode;
        this.errMsg = errMsg;
    }
}

这是我的服务:

@Override
@Transactional(rollbackOn=CustomerGenericException.class)
public Customer registerCustomer(String name, String email, String password) {
    LOGGER.info("Persist a new Customer Entry");
    long startTime = System.currentTimeMillis();

    Login login = new Login();      
    if( login.validateUsername(name) && login.validateEmail(email)
            && login.validatePassword(password)) {
        Customer customer = new Customer();

        // Set login account
        login.setName(name);
        login.setEmail(email);
        login.setPassword(encodedPassword(password));

        customer.setLogin(login);   
        login.setCustomer(customer);
        login.setEnabled(true);

        registerDao.add(customer);
        return customer;
    }
    return null;
}

因此,每当我违反唯一约束时,我都获得了http 500状态,并且在控制台中,它显示重复值违规。如何正确处理spring mvc和hibernate中的异常?

2 个答案:

答案 0 :(得分:0)

您可以创建单独的Controller来处理此类异常。请参阅此链接以供参考:

Exception Handling in Spring MVC,使用@ExceptionHandler

答案 1 :(得分:0)

代码中的异常由以下行引发

Customer aCustomer = registerManager.registerCustomer(username, email, password);

必须是一个hibernate异常,并且永远不会执行此行下面的代码。另一方面,ExceptionHandler只处理CustomerGenericException,它永远不会从您的控制器中抛出。 你有很多方法来处理这个问题,取决于你真正想要它的方式。 对我来说,更好的方法是使用Spring异常翻译,请参阅here

基本上你需要确保你有一个异常处理程序来处理特定类型的异常。