【发布时间】:2019-04-02 18:58:38
【问题描述】:
我有一个 Rest API,我想捕获所有异常,以便在引发错误时为客户端发送自定义消息。
我使用 try { ... } catch (Exception e) { ... } 捕获了异常,但是在这些模式下,不会执行回滚并且数据会被持久化。
@POST
@Transactional
public Response add(Foo foo) {
try {
Foo add = this.service.add(foo);
return Response.status(CREATED)
.entity(add)
.build();
} catch (Exception e) {
return Response.status(BAD_REQUEST)
.entity("Contact the support! Error: " + e.getMessage())
.build();
}
}
我想回滚事务,因为数据不正确。
【问题讨论】: