java补丁udpates和validate requestbody

jei2mxaa  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(275)

我已经学了几周春靴了。我正在用hibernate+jpa和mysql数据库构建一个简单的api。
我有一个资源呼叫TVShowReminderResponsed到:

public class TvShowReminderResponseDTO {
    // Attributes
    private Integer idTvShowReminder;
    private User user;
    private UserTvShow userTvShow;
    private TvShowDetailsResponseDTO tvShowDetailsResponseDTO;
    private Boolean completed;
    private Integer currentSeason;
    private Integer currentEpisode;
    private Integer personalRating;

    // rest of the code omittedfor brevity
}

在我的控制器中,我有一个基本的更新修补程序终结点,它接收存储在我的数据库中的电视节目提醒(实体)的id,并且我还接收一个tvshowreminderpatchdto,其中包含我要更新的信息:
连接到和控制器:

public class TvShowReminderPatchDTO {
    // Attributes
    private Optional<Boolean> completed;
    private Optional<Integer> currentSeason;
    private Optional<Integer> currentEpisode;
    private Optional<Integer> personalRating;
    // rest of the code omittedfor brevity
}

    @PatchMapping("/{idTvShowReminder}")
    public void updateTvShowReminder(@RequestBody @Valid TvShowReminderPatchDTO tvShowReminderToUpdate, 
    @PathVariable  Integer idTvShowReminder){
        tvShowReminderService.updateTvShowReminder(tvShowReminderToUpdate,idTvShowReminder);
    }

我也有我的服务方法,负责搜索tvshowreminder实体的id,然后更新我们从客户端获得的信息。

public void updateTvShowReminder(TvShowReminderPatchDTO tvShowReminderToUpdate, Integer idTvShowReminder) {
        Optional<TvShowReminder> tvShowReminder = getTvShowReminder(idTvShowReminder);
        TvShowReminder currentTvShowReminder = tvShowReminder.get();

        if(tvShowReminderToUpdate.getCompleted() != null) {
            if (tvShowReminderToUpdate.getCompleted().isPresent()) {
                currentTvShowReminder.setCompleted(tvShowReminderToUpdate.getCompleted().get());
            } else {
                currentTvShowReminder.setCompleted(null);
            }
        }

        if(tvShowReminderToUpdate.getCurrentSeason() != null) {
            if (tvShowReminderToUpdate.getCurrentSeason().isPresent()) {
                currentTvShowReminder.setCurrentSeason(tvShowReminderToUpdate.getCurrentSeason().get());
            } else {
                currentTvShowReminder.setCurrentSeason(null);
            }
        }

        if(tvShowReminderToUpdate.getCurrentEpisode() != null) {
            if (tvShowReminderToUpdate.getCurrentEpisode().isPresent()) {
                currentTvShowReminder.setCurrentEpisode(tvShowReminderToUpdate.getCurrentEpisode().get());
            } else {
                currentTvShowReminder.setCurrentEpisode(null);
            }
        }

        if(tvShowReminderToUpdate.getPersonalRating() != null) {
            if (tvShowReminderToUpdate.getPersonalRating().isPresent()) {
                currentTvShowReminder.setPersonalRating(tvShowReminderToUpdate.getPersonalRating().get());
            } else {
                currentTvShowReminder.setPersonalRating(null);
            }
        }

        tvShowReminderRepository.save(currentTvShowReminder);
    }

我对控制器中的@valid注解有一个问题:我以为它会检查我们从postman发送的对象是否是类型tvshowrinderpatchdto的,但是我可以发送一个完全不同的对象,控制器将启动它的执行,并且tvshowrinderpatchdto的所有属性都为null。
检查请求主体是否实际上是tvshowreminderpatchdto的最佳方法是什么?我想验证我们从请求中得到的对象是否是tvshowreminderpatchdto的示例,如果不是,则抛出异常。
做补丁的方法是可行的,但它非常难看,我在tvshowreminderpatchdto中使用可选的属性,这样我就可以区分客户机是想设置一个空值(发送一个带有空值的属性)还是该属性被推荐(它没有出现在请求体上),所以我们不需要做任何事情,意思是我们不更新它。
你们能推荐一个更好的方法来做这个或者改进现有的代码吗?

uinbv5nw

uinbv5nw1#

在dto中使用@notnull注解添加一些必需的字段,以帮助spring理解类型中应该存在哪些属性
不要使用可选的。已存在用于此目的的jsonnullable
公共类tvshowreminderpatchdto{@notnull private jsonnullablecompleted=jsonnullable.undefined();}
在控制器方法中:

if (dto.getCompleted().isPresent()) {
  object.setCompleted(dto.getCompleted().get());
}

就这样,不需要空检查,只需设置值

相关问题