在Restful API中,ResponseEntity
和ResponseEntity<?>
有什么区别?
我们什么时候应该使用其中一个?这是我的代码:
@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);
}