1

I am using Apache CXF and developing a web service and I don't want to return soap fault. If some kind of exception happens inside of the WebMethods i want to catch it inside a interceptor and return OK.

In case of error web service returns something like this but, i don't want to return something like this.

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
   <soap:Body>
      <soap:Fault>
         <soap:Code>
            <soap:Value>soap:Receiver</soap:Value>
         </soap:Code>
         <soap:Reason>
            <soap:Text xml:lang="en">Fault occurred while processing.</soap:Text>
         </soap:Reason>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

i want to edit the response above and return something like below.

    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
       ws-address- ws-security
<soap:Body>
          <Response>
            Ok!
          </Response>
       </soap:Body>
    </soap:Envelope>

I added an out fault interceptor and i can catch the exception inside of it. But if i delete the exception from soap message the web service returns nothing. I can't add new object inside of it.

public class SoapInterceptor extends AbstractSoapInterceptor {

public SoapInterceptor() {
    super(Phase.PRE_STREAM);
}

@Override
public void handleMessage(SoapMessage message) throws Fault {
    Fault fault = (Fault) message.getContent(Exception.class);

    message.removeContent(Exception.class);
    message.setContent(BaseResponse.class, new BaseResponse(ResultCode.SUCCESS));
}}
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
ogunozkan
  • 11
  • 2
  • As I understand from Interceptor, there are phases that can be appropriate for you. https://cxf.apache.org/docs/interceptors.html You can select UNMARSHAL Phase and intercept response. – Gurkan İlleez Jun 23 '20 at 14:26
  • https://stackoverflow.com/questions/10064191/error-handling-with-cxf-interceptors-changing-the-response-message – Gurkan İlleez Jun 23 '20 at 14:30

0 Answers0