spring data rest - Save or Merge Patch Entity -
i want following functionality in spring data rest.
if post collection resource end point, server should check if object exists. if exists should perform same functionality merge-patch on item resource. if object not exist should create it.
is achievable in spring data rest. if how?
if possible in use case, might want use put instead of post, put should work expected.
solution post
you can achieve desired behavior spring data rest event handlers.
just create handler method accepts entity , annotate @handlebeforecreate. in method, can implement behavior, i.e. check if object exists , update manually or nothing , let spring data rest handle entity creation.
@repositoryeventhandler public class entityeventhandler { @autowired private entityservice entityservice; @handlebeforecreate public void handleentitycreate(entity e) { if (entityservice.exists(e)) { entityservice.update(e); } } } edit: realized need stop create event after update. might try throwing custom exception , handling return 200 , updated entity.
Comments
Post a Comment