Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spring Web Service Client Fault Handling

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

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:

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!

like image 477
user544192 Avatar asked Sep 24 '11 20:09

user544192


People also ask

How do you handle SOAP fault in Spring boots?

One way is writing your custom interceptor which implements Spring WS's ClientInterceptor interface. You should override handleFault method to handle SOAP faults with your custom logic. Then you need to register your custom Interceptor class as an interceptor at your SOAP client config class.

How do you handle Defaulthandlerexceptionresolver?

Method SummaryHandle the case where an async request timed out. Handle the case where an @ModelAttribute method argument has binding or validation errors and is not followed by another method argument of type BindingResult . Handle the case when a WebDataBinder conversion cannot occur.


2 Answers

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

like image 61
jddsantaella Avatar answered Oct 05 '22 20:10

jddsantaella


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

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

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 Dispatcher to 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

like image 44
Cratylus Avatar answered Oct 05 '22 20:10

Cratylus