簡體   English   中英

自定義HTTP狀態代碼(已注冊狀態代碼除外)

[英]Custom HTTP Status Code other than Registered Status Code

我正在嘗試使用自定義HTTP狀態代碼(450),但出現錯誤“ org.springframework.http.HttpStatus沒有定義任何枚舉常量'450'”。 我嘗試搜索其他線程,但無法找到適合我情況的適當答復。 任何幫助是極大的贊賞。 謝謝!

請在下面找到我的代碼:

<http:inbound-gateway supported-methods="POST"
        request-channel="headerEnricher"
        error-channel="errorHandlerRouterChannel"
        reply-channel="responseChannel" header-mapper="headerMapper"
        path="/testApi/1.0" request-payload-type="java.lang.String" />

錯誤處理程序方法:

private Message<String> createErrorResponse() {
        Map<String, String> respHeader = new HashMap<String, String>();
        respHeader.put(HttpHeaders.STATUS_CODE, "450");
        return MessageBuilder.withPayload("Validation Error")
                .copyHeaders(respHeader).build();
}

錯誤異常:

HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalArgumentException: No matching constant for [450]
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No matching constant for [450]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:595)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
root cause: java.lang.IllegalArgumentException: No matching constant for [450]
    org.springframework.http.HttpStatus.valueOf(HttpStatus.java:426)
    org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.resolveHttpStatusFromHeaders(HttpRequestHandlingEndpointSupport.java:549)
    org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.setupResponseAndConvertReply(HttpRequestHandlingEndpointSupport.java:443)
    org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:103)
    org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:595)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:668)

a)我認為當Spring僅支持一組硬連接的狀態代碼時,它是有問題的; 這些是HTTP中的擴展點,因此狀態代碼集是開放式的(嗯,直到填滿范圍100..599)。

b)關於您的用例-也許您可以只使用狀態代碼422( https://greenbytes.de/tech/webdav/rfc4918.html#STATUS_422 )?

暫無
暫無

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

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