Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
882 views
in Technique[技术] by (71.8m points)

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

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 an exception is thrown (adding a "detail" tag).

I followed this article to do so : http://www.stevideter.com/2009/02/18/of-exceptionresolvers-and-xmlbeans/

Here is my exception:

package foo.bar.exception;

import org.springframework.ws.soap.server.endpoint.annotation.FaultCode;
import org.springframework.ws.soap.server.endpoint.annotation.SoapFault;

@SoapFault(faultCode = FaultCode.SERVER)
public class ServiceException extends Exception {

    private static final long serialVersionUID = -1804604596179996724L;

    private String tempFaultDetail;

    public ServiceException(){
        super("ServiceException");
    }

    public ServiceException(String message) {
        super(message);
    }

    public ServiceException(String message, Throwable cause) {
        super(message, cause);
    }

    public ServiceException(String message, Throwable cause, String fautDetail) {
        super(message, cause);
        setTempFaultDetail( fautDetail );
    }


    public String getTempFaultDetail() {
        return tempFaultDetail;
    }

    public void setTempFaultDetail(String tempFaultDetail) {
        this.tempFaultDetail = tempFaultDetail;
    }       
}

Here is my beans.xml (I tried to do it with Java configuration and annotation, but I'm not sure I'm doing it right, so I backed up to XML bean declaration):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="exceptionResolver"
        class="foo.bar.ws.DetailSoapFaultDefinitionExceptionResolver">
        <property name="defaultFault" value="SERVER" />
        <property name="exceptionMappings">
            <value>
                foo.bar.exception.ServiceException=SERVER,FaultMsg
            </value>
        </property>
        <property name="order" value="1" />
    </bean>
</beans>

And the custom class I wrote to override SoapFaultAnnotationExceptionResolver (at first I extended SoapFaultMappingExceptionResolver as presented in the article above) :

package foo.bar.ws;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.ws.soap.SoapFault;
import org.springframework.ws.soap.server.endpoint.SoapFaultAnnotationExceptionResolver;

@Component
public class DetailSoapFaultDefinitionExceptionResolver extends
        SoapFaultAnnotationExceptionResolver {

    public final static Logger logger = Logger.getLogger( DetailSoapFaultDefinitionExceptionResolver.class );

    public DetailSoapFaultDefinitionExceptionResolver() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void customizeFault(Object endpoint, Exception ex, SoapFault fault) {
        logger.debug("TEST OK !");
    }

}

But when I throw a ServiceException in my endpoint, the customizeFault method of the custom class is never hit. And for a good reason, the class used as an exception handler is still SoapFaultAnnotationExceptionResolver and not mine...

Does anyone see an explanation ?

Already looked:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...