在 WS SoapFault 中添加详细信息:未使用我的自定义 ExceptionResolver

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

我正在使用 Spring Boot (1.2.4.RELEASE) 构建 Web 服务,我对这个框架还很陌生。 特别是,我正在尝试在抛出异常时自定义 SoapFault 内容(添加 "detail" 标记)。

我是按照这篇文章这样做的:http://www.stevideter.com/2009/02/18/of-exceptionresolvers-and-xmlbeans/

这是我的例外情况:

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

这是我的beans.xml(我试着用Java配置和注释来做,但我不确定我做对了,所以我备份到XML bean 声明):

<?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>

我写的自定义 class 来覆盖 SoapFaultAnnotationExceptionResolver(起初我扩展了 SoapFaultMappingExceptionResolver,如上文所述):

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 !");
    }

}

但是当我在端点中抛出 ServiceException 时,自定义 class 的 customizeFault 方法永远不会被命中。有充分的理由,用作异常处理程序的 class 仍然是 SoapFaultAnnotationExceptionResolver 而不是我的...

有人看到解释了吗?

已看过:

像往常一样,我在将问题发布到网上一小时后解决了问题。我应该早点做的!

我试图覆盖的 bean name/id 不正确。在扫描了大量org.springframework.beans调试日志后,我发现正确的bean名称是soapFaultAnnotationExceptionResolver

我还设法将配置转换为 Java 形式:

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

}

我知道此回复已过时,但也许对其他人有用。

MessageDispatcher 默认引发两个 Resolver:SimpleSoapExceptionResolver 和 SoapFaultAnnotationExceptionResolver,如果您想使用自定义代码和错误消息获取 soap 错误,则必须声明正确的顺序以首先获取 SoapFaultAnnotationExceptionResolver,然后是 SimpleSoapExceptionResolver。

第一步.bean配置文件对应:

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

第 2 步。声明您的异常如下:

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

步骤 3.

正常在代码中引发异常

抛出新的 DeclaracionNotFoundException(); //使用来自 faultStringOrReason 的默认消息 要么 抛出新的 DeclaracionNotFoundException("Ops!!!"); //与其他消息

它对我有用,我得到了以下信息:

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

我定义了 faultCode = 4 和 faultString=Ops !!!

此致