英文:
HTTP Status in case of EntityNotFoundException
问题
我正在处理我的应用程序中的异常情况。在我的服务中,我有这个方法:
public Optional<Entity> getEntityById(Long id) {
return Optional.of(EntityMapper.fromEntityToDto(repository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException("找不到id为" + id + "的条目")));
}
在我的异常处理程序中,我有以下内容:
@ControllerAdvice
public class ControllerAdvisor extends ResponseEntityExceptionHandler {
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<Object> handleEntityNotFoundException(EntityNotFoundException ex, WebRequest request) {
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.NOT_FOUND, "找不到实体", ex.getMessage());
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
}
我看到过一些示例,他们在这里1和这里2这样做,但在我看来,这似乎是对HttpStatus
的滥用。资源是API端点(这没问题),而不是未找到的实体。如何处理这种情况会是最好的方式?
根据IQbrod的答案,我创建了一个异常:
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public class MyEntityNotFoundException extends RuntimeException {
public MyEntityNotFoundException(String message) {
super(message);
}
}
我现在抛出这个异常,而不是EntityNotFoundException
。
英文:
I'm working on the exception handling in my application. In my service I have this method:
public Optional<Entity> getEntityById(Long id) {
return Optional.of(EntityMapper.fromEntityToDto(repository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException("No entry was found for" + " id: " + id))));
}
In my exception handler I have the following
@ControllerAdvice
public class ControllerAdvisor extends ResponseEntityExceptionHandler {
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<Object> handleEntityNotFoundException(EntityNotFoundException ex, WebRequest request) {
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.NOT_FOUND, "Entity not found", ex.getMessage());
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
}
I have seen examples where they do this here and here but it seems to me like an abuse of the HttpStatus
. The resource is the API endpoint (which is fine) and not the entity that was not found. What would be the best way to handle this use case?
Based on IQbrod's answer I created an exception:
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public class MyEntityNotFoundException extends RuntimeException {
public MyEntityNotFoundException(String message) {
super(message);
}
}
which I now throw instead of the EntityNotFoundException
答案1
得分: 5
永恒的辩论:404
与204
。
让我们试着通过一个例子来说明。您的实体现在是一个装满神奇物体的盒子
。
盒子1:里面有一只漂亮的小马的盒子。
盒子2:一个空盒子。
让我们来查询第一个盒子的内容:
GET https://{URL}/v1/boxes/1/content
- 200(小马)
现在让我们查询第二个盒子的内容:
GET https://{URL}/v1/boxes/2/content
- 204 无内容
现在让我们查询第3号盒子(盒子本身,而不是内容):
GET https://{URL}/v1/boxes/3
- 204 无内容,在您的存储中找不到编号为3的任何盒子
现在让我们查询第3号盒子里面装着什么:
GET https://{URL}/v1/boxes/3/content
- 404 盒子不存在。
这里的404意味着无法找到资源,用户没有检查盒子是否存在,他的请求是没有意义的(基本上是4xx)。
附注: 您可能对此有不同的看法,这只是我的解释。
英文:
The eternal debate between 404
and 204
.
Let's try to have an example here. Your entity is now a box
filled with magical objects.
Box1 : It's a box with a beautiful pony inside.
Box2 : It's an empty box.
Let's ask for the first box content :
GET https://{URL}/v1/boxes/1/content
- 200 (Pony)
Now let's ask for the content of the second box :
GET https://{URL}/v1/boxes/2/content
- 204 NO CONTENT
Let's ask for the box n°3 (the box itself, not the content):
GET https://{URL}/v1/boxes/3
- 204 NO CONTENT, In your storage you couldn't find any box with number 3
Let's now ask what's inside box n°3:
GET https://{URL}/v1/boxes/3/content
- 404 the box doesn't exists.
Here 404 does mean that the resource couldn't be found, user didn't check if the box exists and his request is nonsensed (which is basically 4xx).
PS: You might have a different opinion on the subject, this is only my interpretation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论