3 Replies Latest reply on Dec 20, 2013 8:23 AM by pvivacqua

    Sending Custom Response for CXF webservice

    kishor_pawar89

      Hi,

       

      How to send custom SoapFault message to the client?

      I am currently getting this kind of response when i throw SoapFault.

       

      <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

         <soap:Body>

            <soap:Fault>

               <faultcode>soap:test</faultcode>

               <faultstring>org.apache.cxf.binding.soap.SoapFault: Something went wrong</faultstring>

            </soap:Fault>

         </soap:Body>

      </soap:Envelope>

       

       

      I would like to send response as,

       

      <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

      <soap:Body>

           <Response>

                  <Data>

                  <Status>Failure</Status>

                  <ErrorMessage>  ………  </ErrorMessage>

           </Response>

         </soap:Body>

      </soap:Envelope>

        • 1. Re: Sending Custom Response for CXF webservice
          jim.ma

          You can either re-write your sevice endpoint inerface/implementation to return a response object you want , or configure the cxf transformfeature to modify the fault response : http://cxf.apache.org/docs/transformationfeature.html

          • 2. Re: Sending Custom Response for CXF webservice
            pbielicki

            Hi Kishor,

             

            did you solve this issue? I'm having the same issue, and am figthing with CFX at the moment not to send soap:Fault but a custom body.

             

            Cheers,

            Przemyslaw

            • 3. Re: Sending Custom Response for CXF webservice
              pvivacqua

              Hello Kishor,

               

              In our CXF implementations we usually return a Response object with a customized message from a message.properties file depending on the exceptions that our WS throws. In a very simplified way, you might do something like this.

               

              In our SEI we have a method that calls our meditator

              public ResponseTest doTheThing(RequestTest requestTest) {
              
                ResponseTest responseTest = thingMediator.add(requestTest);
                return response;
              
                }
              

               

              mediator

              public ResponseTest add(RequestTest requestTest) {
              
              
                ResponseTest responseTest = new ResponseTest();
              
                try {
              
                ... calls your business rules ...
                
                } catch (Exception se) {           
                      
                      return responseTest.setMessage("Something went wrong");
              
                }
              
              
                return responseTest.setMessage("Success");
              
                }
              

               

              response class

              public class ResponseTest {
              
              
                private String message
              
                public String getMessage(){
                return message;
                }
              
                public void setMessage(String message){
                this.message = message
                }
                
              {