spring-boot - How to deal with ContraintViolationException and other Exceptions
问题描述
I have a camel route like this:
this.from(uri).process(e -> {
String json = e.getIn().getBody(String.class);
JsonObject message = gson.fromJson(json, JsonObject.class);
Status status = gson.fromJson(message.get("data"), Status.class);
e.getIn().setHeader("JSON_OBJECT", message);
e.getIn().setBody(status);
}).to(jpaStatusUri).process(e -> {
JsonObject message = (JsonObject) e.getIn().getHeader("JSON_OBJECT");
StatusDetails details = gson.fromJson(message.get("data"), StatusDetails.class);
// If the first route was successfull i can get the id of the inserted entity here (this works)
details.setStatusId(e.getIn().getBody(Status.class).getId());
e.getIn().setBody(details);
}).to(jpaDetailsUri);
I send the entity Status
to a jpa endpoint jpaStatusUri
. The entity is inserted, and in the following process()
method i create a corresponding StatusDetails
entity, set it's statusId to the id of the previously saved Status
and send it to the jpaDetailsUri
endpoint.
This works as expected in case the Status
entity has been saved successfully. In case of a ConstraintViolationException
i.e. a unique key for the Status
entity already exists this will throw the exception, i will not have a correct statusId
and i will not be able to update the corresponding StatusDetails
entity anymore.
Of course i could all handle this in the process() method, but what would be the "camel-way" to handle this ?
解决方案
推荐阅读
- java - 如何使用 XAdES4j 对 xml 进行签名,并引用 URI (#DatosEmision)?
- android - android片段视图即使在onCreateView中初始化后也会抛出空指针异常
- python - ValueError:从文本文件解析列表时出现格式错误的节点或字符串错误
- apache-spark - 根据 PySpark 中的时区将 UTC 时间戳转换为本地时间
- github-actions - 从 git 操作容器推送时,Git 推送操作不起作用
- java - Java最大函数递归
- google-apps-script - 谷歌表格脚本无法识别拆分功能
- database - 制定 MongoDB 查询
- reactjs - TypeError:无法读取未定义的属性“包含” - 成功编译后 chrome 中的错误
- ios - 如何在以下用例的通知扩展和应用程序之间进行通信?