ResponseEntity和ResponseEntity <有什么区别?

时间:2019-09-27 10:00:09

标签: rest

在Restful API中,ResponseEntityResponseEntity<?>有什么区别?

我们什么时候应该使用其中一个?这是我的代码:

@PutMapping
public ResponseEntity<?> updateArticle(@PathVariable("slug") String slug,
                                       @AuthenticationPrincipal User user,
                                       @Valid @RequestBody UpdateArticleParam updateArticleParam) {
    return articleRepository.findBySlug(slug).map(article -> {
        if (!AuthorizationService.canWriteArticle(user, article)) {
            throw new NoAuthorizationException();
        }
        article.update(
            updateArticleParam.getTitle(),
            updateArticleParam.getDescription(),
            updateArticleParam.getBody());
        articleRepository.save(article);
        return ResponseEntity.ok(articleResponse(articleQueryService.findBySlug(slug, user).get()));
    }).orElseThrow(ResourceNotFoundException::new);
}

@DeleteMapping
public ResponseEntity deleteArticle(@PathVariable("slug") String slug,
                                    @AuthenticationPrincipal User user) {
    return articleRepository.findBySlug(slug).map(article -> {
        if (!AuthorizationService.canWriteArticle(user, article)) {
            throw new NoAuthorizationException();
        }
        articleRepository.remove(article);
        return ResponseEntity.noContent().build();
    }).orElseThrow(ResourceNotFoundException::new);
}

0 个答案:

没有答案