Avoid saving data in spring data rest in handleBeforeSave

浪尽此生 提交于 2019-12-11 23:52:48

问题


I am using @RepositoryRestResource for Mongo Repositories. I want to do some pre-save check and if those checks are not meeting the requirements, I want to abandon the save operation.

I tried :

    @HandleBeforeSave
    public void handleBeforeSave(CallLog callLog)
            throws InternalServerException {

    CallLog log = callRepository.findOne(callLog.getConferenceId());
    if (log != null
            && (callLog.getStatus().equals(Constants.CALL_IN_PROGRESS) || callLog
                    .getStatus().equals(Constants.CALL_DROPPED))) {
        if (callLog.getStatus().equals(Constants.CALL_DROPPED)) {
            User user = userRepository.findOne(callLog.getReceiverId());
            user.setStatus(Constants.USER_STATUS_IDLE);
            userRepository.save(user);
        }

        throw new InternalServerException(
                "This call has already been received");
    } else {
        User user = userRepository.findOne(callLog.getReceiverId());
        user.setStatus(Constants.USER_STATUS_BUSY);
        userRepository.save(user);
    }

}

But throwing exception, does not actually abandon the save call. Is there any other way to do it?


回答1:


You can use a RestController to intercept the request.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html



来源:https://stackoverflow.com/questions/33001518/avoid-saving-data-in-spring-data-rest-in-handlebeforesave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!