在出现EntityNotFoundException时的HTTP状态码。

huangapple go评论91阅读模式
英文:

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&lt;Entity&gt; getEntityById(Long id) {
    return Optional.of(EntityMapper.fromEntityToDto(repository
            .findById(id)
            .orElseThrow(() -&gt; new EntityNotFoundException(&quot;No entry was found for&quot; + &quot; id: &quot; + id))));
}

In my exception handler I have the following

@ControllerAdvice
public class ControllerAdvisor extends ResponseEntityExceptionHandler {

    @ExceptionHandler(EntityNotFoundException.class)
    public ResponseEntity&lt;Object&gt; handleEntityNotFoundException(EntityNotFoundException ex, WebRequest request) {

        ErrorResponse errorResponse = new ErrorResponse(HttpStatus.NOT_FOUND, &quot;Entity not found&quot;, ex.getMessage());
        return new ResponseEntity&lt;&gt;(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

永恒的辩论:404204
让我们试着通过一个例子来说明。您的实体现在是一个装满神奇物体的盒子
盒子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.

huangapple
  • 本文由 发表于 2020年10月9日 18:59:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64278701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定