我正在使用Spring Boot(1.2.4.RELEASE)构建一个Web服务,并且对这个框架还很陌生。特别是, 当引发异常 (添加“详细信息”标签) 时 , 我试图自定义SoapFault内容 。
我按照本文进行操作: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>
还有我编写的用于重写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时,自定义类的customFault方法永远不会被击中。并且有一个很好的理由,用作异常处理程序的类仍然是SoapFaultAnnotationExceptionResolver而不是我的…
有人看到解释吗?
和往常一样,我在在线发布一个小时后解决了我的问题。我应该早点做!
我尝试覆盖的Bean名称/ ID不正确。扫描了大量的org.springframework.beans调试日志后,我发现正确的bean名称为soapFaultAnnotationExceptionResolver。
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; } }