简体   繁体   中英

Custom HTTP Status Code other than Registered Status Code

I am trying to use a custom HTTP Status Code (450) but I get an error "org.springframework.http.HttpStatus doesn't have any enum constant '450' defined". I tried searching the other threads, but I wasn't able to find a suitable reply to my situation. Any help is greatly appreciated. Thanks!

Please find below my code:

<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" />

Error handler method:

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();
}

Error Exception:

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) I would argue that Spring is buggy when it only supports a hard-wired set of status codes; these are an extension point in HTTP and thus the set of status code is open-ended (well, until the range 100..599 has been filled up).

b) With respect to your use case -- maybe you could just use status code 422 instead ( https://greenbytes.de/tech/webdav/rfc4918.html#STATUS_422 )?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM