Java Spring Web 服务客户端故障处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7541836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:24:06  来源:igfitidea点击:

Java Spring Web Service Client Fault Handling

javaweb-servicesspringjaxbspring-ws

提问by user544192

I have written a web service client (using Java Spring and JAXB Marshaller) that works with the UPS web service. When I send a valid request everything works well. When I send an invalid request (weight > 150 lbs) then the UPS web service responds with a SOAP Fault. The client application just fails with a

我编写了一个与 UPS Web 服务配合使用的 Web 服务客户端(使用 Java Spring 和 JAXB Marshaller)。当我发送有效请求时,一切正常。当我发送无效请求(重量 > 150 磅)时,UPS Web 服务会响应 SOAP 故障。客户端应用程序只是失败了

org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling 
exception; nested exception is javax.xml.bind.UnmarshalException: 
unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Fault").

Obviously my program isn't able to decipher the SOAP fault returned by the web service. I wrote a custom FaultMessageResolver, but it doesn't get invoked. Here's the code:

显然,我的程序无法破译 Web 服务返回的 SOAP 错误。我写了一个自定义的 FaultMessageResolver,但它没有被调用。这是代码:

public class UpsRateClient extends WebServiceGatewaySupport {
    public UpsRateClient(WebServiceMessageFactory messageFactory) {
        super(messageFactory);
        getWebServiceTemplate().setFaultMessageResolver(new UpsFaultMessageResolver());
    }

    public RateResponse getRate(RateRequest rateRequest) {
        return (RateResponse) getWebServiceTemplate().marshalSendAndReceive(rateRequest, new UpsRequestWSMC());
    }
    private class UpsFaultMessageResolver implements FaultMessageResolver {
        public void resolveFault(WebServiceMessage message) throws IOException{
            System.out.println("Inside UpsFaultMessageResolver");   
        }
    }
}

Thanks for your time!

谢谢你的时间!

采纳答案by Cratylus

Take a wireshark trace.My thought is that perhaps the web service sends the SOAP fault using (erroneously) a HTTP 200OK instead of a 500 Internal Server error and your client tries to handle it as a valid response.
If this is the case this is then the problem lies in the web service which does not addere to SOAP standard(my emphasis):
From SOAP RFC

进行wireshark 跟踪。我的想法是,也许Web 服务使用(错误地)HTTP 200OK 而不是500 内部服务器错误发送SOAP错误,并且您的客户端尝试将其作为有效响应进行处理。
如果是这种情况,那么问题就出在不符合 SOAP 标准的 Web 服务中(我的重点):
来自SOAP RFC

In case of a SOAP error while processing the request, the SOAP HTTP server MUST issue an HTTP 500 "Internal Server Error" responseand include a SOAP message in the response containing a SOAP Fault element (see section 4.4) indicating the SOAP processing error.

如果在处理请求时发生 SOAP 错误,SOAP HTTP 服务器必须发出 HTTP 500“内部服务器错误”响应,并在响应中包含一个 SOAP 消息,该消息包含一个 SOAP Fault 元素(参见第 4.4 节),指示 SOAP 处理错误。

If you do not own the web service, they should fix this.
To be honest I am not sure what the work arround would be in Spring-WS.
If I really needed a work arround in Jax-Ws I would replace the stub call with a Dispatcherto handle the raw xml myself and avoid the automatic marshal/demarhal.
Look into Spring-Ws if you can do the same, but this is a bug of the web service, not your client

如果您不拥有 Web 服务,他们应该解决此问题。
老实说,我不确定 Spring-WS 中的工作会是什么。
如果我真的需要在 Jax-Ws 中工作,我会用Dispatcher替换存根调用来自己处理原始 xml 并避免自动编组/解组。
如果您可以这样做,请查看 Spring-Ws,但这是 Web 服务的错​​误,而不是您的客户端

回答by jddsantaella

I had the same problem (SOAP error with HTTP 200OK) and I solved it setting the CheckConnectionForFault property to false. See.

我遇到了同样的问题(HTTP 200OK 的 SOAP 错误),我解决了它,将 CheckConnectionForFault 属性设置为 false。