Change Spring WS Error Status Code from 500 to 400

I created a Spring WS application (based on Spring WS 2.2.3) that provides a small two-operation web service. Each operation receives input parameters to search the database and return a response. Some of the parameters are required (for example, street name), and the Client requested that if there is no request in the service, some of these required parameters (for example, empty street name), my service should return a proper SOAP error with HTTP status 400.

My exception handling works fine, and I can return the correct SOAP error to the client if any necessary parameter was missing from the request message, and Spring WS takes care of everything else by wrapping SOAP Fault and sending it it returns to the client with a status code 500, as shown below :

HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
SOAPAction: ""
Content-Type: text/xml;charset=utf-8
Content-Length: 503
Date: Thu, 10 Dec 2015 22:28:02 GMT

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <SOAP-ENV:Fault>
            <faultcode>SOAP-ENV:Client</faultcode>
            <faultstring xml:lang="en">Street Name is required</faultstring>
        </SOAP-ENV:Fault>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

This is good, except that I really want the HTTP status code to be '400 Bad Request' instead of an Internal Server Error . I cannot figure out how to change the status code from 500 to 400 and get a similar answer, as shown below:

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
SOAPAction: ""
Content-Type: text/xml;charset=utf-8
Content-Length: 503
Date: Thu, 10 Dec 2015 22:28:02 GMT

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <SOAP-ENV:Fault>
            <faultcode>SOAP-ENV:Client</faultcode>
            <faultstring xml:lang="en">Street Name is required</faultstring>
        </SOAP-ENV:Fault>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Any ideas?

+4
source share

Source: https://habr.com/ru/post/1619414/


All Articles