在CXF SOAP Fault中更改HTTP响应代码

时间:2013-01-03 20:13:15

标签: soap exception-handling cxf

我有这个错误

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <soap:Fault>
      <faultcode>soap:Client</faultcode>
      <faultstring>Message part {http://soap.ws.server.wst.fit.cvut.cz/}createOrders was not recognized.  (Does it exist in service WSDL?)</faultstring>
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

这完全没问题。但问题是CXF正在返回内部服务器错误(HTTP 500)。我希望它返回400 Bad Request,因为错误的请求导致了这一点。我怎么能改变它?

详情

产生上述错误的错误请求 - 应该是<soap:createOrder/>而不是<soap:createOrders/>

POST http://localhost:8080/wst-server-1.0.0/services/soap/order

POST data:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://soap.ws.server.wst.fit.cvut.cz/">
   <soapenv:Header/>
   <soapenv:Body>
      <soap:createOrders/>
   </soapenv:Body>
</soapenv:Envelope>

[no cookies]

Request Headers:
Content-Length: 238
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded

1 个答案:

答案 0 :(得分:0)

我知道这对于答案可能为时已晚,但另一种处理 HTTP 状态代码的方法是使用 CXF 拦截器。

您可以在您的服务器 CXF 配置中添加拦截器

 public class ServiceConfigurer implements CxfConfigurer {

    @Override
    public void configureServer(Server server) {
         server.getEndpoint().getOutFaultInterceptors().add(new ChangingStatusCodeInterceptor();
    }
 }  

在你的拦截器中,你可以改变这个

public class SaveInfluxDataInterceptor extends WSS4JOutInterceptor {

    @Override
    public void handleMessage(SoapMessage soapMessage) {
        ((Fault)exchange.get(Exception.class)).setStatusCode(202); // this is because of soap12OutFaultInterceptor look to this fault
        // or you can use this
        // exchange.getOutFaultMessage().put(Message.RESPONSE_CODE, 202);
    }
}  

我将 Camel 与 CXF 一起使用。也许这个解决方案应该改变你的。

相关问题