簡體   English   中英

如何處理 Spring Boot 重定向到 /error?

[英]How to handle Spring Boot's redirection to /error?

我遇到了與這個問題相同的問題,使用 Spring Boot 1.3.0 並且沒有用@RestController注釋我的控制器,只是@Path@Service 正如該問題中的 OP 所說,

對我來說,這絕不是明智的

我也不明白他們為什么要把它重定向到/error。 而且很可能我遺漏了一些東西,因為我只能將 404 或 200 回饋給客戶。

我的問題是他的解決方案似乎不適用於 1.3.0,所以我有以下請求流:假設我的代碼拋出NullPointerException 它將由我的ExceptionMapper之一處理

@Provider
public class GeneralExceptionMapper implements ExceptionMapper<Throwable> {

    private static final Logger LOGGER = LoggerFactory.getLogger(GeneralExceptionMapper.class);

    @Override
    public Response toResponse(Throwable exception) {
        LOGGER.error(exception.getLocalizedMessage());
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

我的代碼返回 500,但它沒有將其發送回客戶端,而是嘗試將其重定向到 /error。 如果我沒有其他資源,它會發回 404。

2015-12-16 18:33:21.268  INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter  : 1 * Server has received a request on thread http-nio-8080-exec-1
1 > GET http://localhost:8080/nullpointerexception
1 > accept: */*
1 > host: localhost:8080
1 > user-agent: curl/7.45.0

2015-12-16 18:33:29.492  INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter  : 1 * Server responded with a response on thread http-nio-8080-exec-1
1 < 500

2015-12-16 18:33:29.540  INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter  : 2 * Server has received a request on thread http-nio-8080-exec-1
2 > GET http://localhost:8080/error
2 > accept: */*
2 > host: localhost:8080
2 > user-agent: curl/7.45.0

2015-12-16 18:33:37.249  INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter  : 2 * Server responded with a response on thread http-nio-8080-exec-1
2 < 404

和客戶端(卷曲):

$ curl -v http://localhost:8080/nullpointerexception
* STATE: INIT => CONNECT handle 0x6000572d0; line 1090 (connection #-5000)
* Added connection 0. The cache now contains 1 members
*   Trying ::1...
* STATE: CONNECT => WAITCONNECT handle 0x6000572d0; line 1143 (connection #0)
* Connected to localhost (::1) port 8080 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x6000572d0; line 1240 (connection #0)
* STATE: SENDPROTOCONNECT => DO handle 0x6000572d0; line 1258 (connection #0)
> GET /nullpointerexception HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.45.0
> Accept: */*
>
* STATE: DO => DO_DONE handle 0x6000572d0; line 1337 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x6000572d0; line 1464 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x6000572d0; line 1474 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 404 Not Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Length: 0
< Date: Wed, 16 Dec 2015 17:33:37 GMT
<
* STATE: PERFORM => DONE handle 0x6000572d0; line 1632 (connection #0)
* Curl_done
* Connection #0 to host localhost left intact

所以它總是 404。除非我確實有這樣的 /error 資源,然后呢? 我應該返回什么? 那時我所擁有的只是對 /error 的 GET 請求。 而且我不希望這些額外的請求消耗資源並污染我的日志。

我錯過了什么? 如果沒有,我應該如何處理異常處理?

您可以將 Jersey 屬性ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR設置為true

當響應狀態為4xx5xx ,可以在容器特定的響應實現上在sendErrorsetStatus之間進行選擇。 例如,在 servlet 容器 Jersey 上可以調用HttpServletResponse.setStatus(...)HttpServletResponse.sendError(...)

調用sendError(...)方法通常會重置實體、響應頭並為指定的狀態代碼(例如 servlet error-page配置)提供錯誤頁面。 但是,如果您想對響應進行后處理(例如通過 servlet 過濾器),唯一的方法是在容器 Response 對象上調用setStatus(...)

如果屬性值為 true,則Response.setStatus(...)方法用於默認Response.sendError(...)

屬性值的類型是boolean 默認值為false

您可以通過在ResourceConfig子類構造函數中調用property(key, value)來設置 Jersey 屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM