【问题标题】:java.sql.SQLIntegrityConstraintViolationException Spring bootjava.sql.SQLIntegrityConstraintViolationException 春季启动
【发布时间】:2019-09-03 23:22:05
【问题描述】:

我该如何处理这个异常

这是为了从 JPA 持久性中捕获错误

try {
    if (platform.getPlatformId() == null) {
        messageString = "inserted";
    } else {
        messageString = "updated";
    }

    Platform platf = platformServiceImpl.addOne(platform);
    model.addAttribute("locations", locationServiceImpl.getAll());

    //Date localDate = new Date();
    //PlatformDetail newDetailPlatform = new PlatformDetail();
    //newDetailPlatform.setLastUpdate(localDate);
    //newDetailPlatform.setPlatform(platform);
    //platformDetailServiceImpl.addOne(newDetailPlatform);
    //redirectAttributes.addAttribute("platformId", platf.getPlatformId());
    redirectAttributes.addFlashAttribute("success", messageString);
    return "redirect:/admin/step1/" + platf.getPlatformId();

} catch (PersistenceException ex) {
    redirectAttributes.addFlashAttribute("error", "dfdf");
    return "redirect:/admin/step1/" + ex.getCause();
}

我希望发送带有重复键的 RedirectParameter 并显示为消息

【问题讨论】:

    标签: java spring-boot


    【解决方案1】:

    catch 部分中,仅重定向到/admin/step1 或其他带有出错消息的端点。添加ex.getCause() 没有意义,因为getCause() 返回一个Throwable 对象(准确地说:对象实现Throwable 接口)。

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 2015-04-18
      • 2017-06-24
      • 2015-08-22
      • 2015-09-18
      • 2015-03-20
      • 2018-01-24
      • 2016-08-22
      • 2016-07-22
      相关资源
      最近更新 更多