Adding detail in a WS SoapFault : my custom ExceptionResolver is not used

后端 未结 2 951
囚心锁ツ
囚心锁ツ 2020-12-10 19:29

I\'m building a web service using Spring Boot (1.2.4.RELEASE) and I\'m quite new to this framework. Especially, I\'m trying to customize the SoapFault content when a

相关标签:
2条回答
  • 2020-12-10 20:25

    I know that this response is outdated but maybe it works for someone else.

    MessageDispatcher raise two Resolvers by default: SimpleSoapExceptionResolver and SoapFaultAnnotationExceptionResolver in that order, if you want to get a soap fault with custom code and error message, you must declare the correct order to get SoapFaultAnnotationExceptionResolver first, and then SimpleSoapExceptionResolver.

    Step 1. On bean configuration file corresponding:

    <bean   class="org.springframework.ws.soap.server.endpoint.SoapFaultAnnotationExceptionResolver">
        <property name="order" value="1"/>
    </bean>
    
    <bean class="org.springframework.ws.soap.server.endpoint.SimpleSoapExceptionResolver">
        <property name="order" value="2"/>
    </bean>
    

    Step 2. Declare your Exceptions as follows:

    @SoapFault(faultCode = FaultCode.CUSTOM,locale="en",faultStringOrReason = "CUSTOM_MESSAGE",customFaultCode="YOUR_NAMESPACE + YOUR_CUSTOM_CODE")
    
    public class DeclaracionNotFoundException extends BusinessException {
    
    public DeclaracionNotFoundException(){
        super();
    }
    
    public DeclaracionNotFoundException(String message) {
        super(message);
    }
    }
    

    Step 3.

    Raise your Exception in your code normally

    throws new DeclaracionNotFoundException(); //With default message from faultStringOrReason or throws new DeclaracionNotFoundException("Ops!!!"); //With other message

    It works for me, and I got the following:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
          <SOAP-ENV:Fault>
             <faultcode xmlns:ns0="http://com.example">ns0:4</faultcode>
             <faultstring xml:lang="en">Ops !!!</faultstring>
          </SOAP-ENV:Fault>
       </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    I defined faultCode = 4 and faultString=Ops !!!

    Regards

    0 讨论(0)
  • 2020-12-10 20:26

    As usual, I solve my problem an hour after posting it online. I should have done it earlier !

    The bean name/id I tried to override was not right. After scanning a huge amount of org.springframework.beans debug logs, I found that the right bean name is soapFaultAnnotationExceptionResolver.

    I also managed to convert the configuration in Java form:

    package foo.bar.ws;
    
    // Skipping imports...
    
    /**
     * WS configuration and WSDL definition
     */
    @EnableWs
    @Configuration
    public class WebServiceConfig extends WsConfigurerAdapter {
    
        public final static Logger logger = Logger.getLogger( WebServiceConfig.class );
    
        // Skipping other bean declarations...
    
        @Bean(name = "soapFaultAnnotationExceptionResolver")
        public DetailSoapFaultDefinitionExceptionResolver exceptionResolver( ApplicationContext applicationContext ){
            DetailSoapFaultDefinitionExceptionResolver exceptionResolver = new DetailSoapFaultDefinitionExceptionResolver();
    
            SoapFaultDefinition soapFaultDefinition = new SoapFaultDefinition();
            soapFaultDefinition.setFaultCode( SoapFaultDefinition.SERVER );
            exceptionResolver.setDefaultFault( soapFaultDefinition );
    
            return exceptionResolver;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题