为什么Spring Boot处理" 404 Not Found"对于实体不同?

时间:2017-02-20 12:13:42

标签: java spring spring-mvc spring-boot spring-data-rest

我想知道Spring Boot为什么处理404 Not Found。

无现有路径示例

我使用curl在没有现有路径上发出请求:

$ curl -v -H "Authorization: Basic YWRtaW46YWRtaW4xMjM=" http://localhost:8080/no/endpoint | python -m json.tool
* Adding handle: conn: 0x69aa30
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x69aa30) send_pipe: 1, recv_pipe: 0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /no/endpoint HTTP/1.1
> User-Agent: curl/7.33.0
> Host: localhost:8080
> Accept: */*
> Authorization: Basic YWRtaW46YWRtaW4xMjM=
>
< HTTP/1.1 404
< Set-Cookie: JSESSIONID=62B5B02F18842F3BD46BCE57F2EAB017;path=/;HttpOnly
< X-Application-Context: Rechnungsservice Gateway:dev
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Mon, 20 Feb 2017 11:59:19 GMT
<
{ [data not shown]
100   115    0   115    0     0    569      0 --:--:-- --:--:-- --:--:--   614
* Connection #0 to host localhost left intact
{
    "error": "Not Found",
    "message": "No message available",
    "path": "/no/endpoint",
    "status": 404,
    "timestamp": 1487591959599
}

与浏览器的操作相同: enter image description here

现在我请求一个没有现有实体的端点:

$ curl -v -H "Authorization: Basic YWRtaW46YWRtaW4xMjM=" http://localhost:8080/api/v1/settings/123 | python -m json.tool                                                                                                                    

* Adding handle: conn: 0x62aa40                                                                                       
* Adding handle: send: 0                                                                                              
* Adding handle: recv: 0                                                                                              
* Curl_addHandleToPipeline: length: 1                                                                                 
* - Conn 0 (0x62aa40) send_pipe: 1, recv_pipe: 0                                                                      
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                       
                                 Dload  Upload   Total   Spent    Left  Speed                                         
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* About to connect() to localhost port 8
080 (#0)                                                                                                              
*   Trying 127.0.0.1...                                                                                               
* Connected to localhost (127.0.0.1) port 8080 (#0)                                                                   
> GET /api/v1/settings/123 HTTP/1.1                                                                                   
> User-Agent: curl/7.33.0                                                                                             
> Host: localhost:8080                                                                                                
> Accept: */*                                                                                                         
> Authorization: Basic YWRtaW46YWRtaW4xMjM=                                                                           
>                                                                                                                     
< HTTP/1.1 404                                                                                                        
< Set-Cookie: JSESSIONID=BCD5ADDA48EB03C235E6573A36860F7D;path=/;HttpOnly                                             
< X-Application-Context: Rechnungsservice Gateway:dev                                                                 
< X-Content-Type-Options: nosniff                                                                                     
< X-XSS-Protection: 1; mode=block                                                                                     
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate                                                       
< Pragma: no-cache                                                                                                    
< Expires: 0                                                                                                          
< Content-Length: 0                                                                                                   
< Date: Mon, 20 Feb 2017 12:05:29 GMT                                                                                 
<                                                                                                                     
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0                                        
* Connection #0 to host localhost left intact                                                                         
No JSON object could be decoded    

浏览器中的相同操作: enter image description here

所以我的问题是:为什么Spring Boot仅在未找到实体的情况下发送简单的404状态?我想看到一个很好的消息:在我的错误页面上找不到实体或者类似这样的文本/ html或者在json请求的情况下的json错误对象...对于路径示例它的工作方式就像一个魅力盒子......

我正在使用Spring Boot 1.4.3.RELEASE。

更新 对不起,我忘了说我正在使用Spring Data Rest。 在这里我调试了unti我发现了这段代码:

class RepositoryEntityController ... {

public ResponseEntity<Resource<?>> getItemResource(...) {

Object domainObj = getItemResource(resourceInformation, id);

    if (domainObj == null) {
        return new ResponseEntity<Resource<?>>(HttpStatus.NOT_FOUND);
    }

}

在我看来,对不存在的单个实体的请求应该抛出&#34; NotFoundException&#34;,从而产生更好的响应。

1 个答案:

答案 0 :(得分:2)

第一个请求永远不会到达您的Spring应用程序,您的WAR未部署在该位置,因此您可以从Web服务器(也许是Tomcat)获取通用404消息。