我正在使用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时,自定义类的customizeFault方法永远不会被命中。并且有充分的理由,用作异常处理程序的类仍然是SoapFaultAnnotationExceptionResolver而不是我的......
有人看到解释吗?
已经看过了:
答案 0 :(得分:7)
像往常一样,我在网上发帖后一小时就解决了我的问题。我应该早点做完!
我试图覆盖的bean名称/ 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;
}
}
答案 1 :(得分:2)
我知道这个回复已经过时但可能对其他人有效。
MessageDispatcher默认情况下引发两个Resolvers:SimpleSoapExceptionResolver和SoapFaultAnnotationExceptionResolver按顺序,如果你想获得带有自定义代码和错误消息的soap错误,你必须先声明正确的顺序才能获得SoapFaultAnnotationExceptionResolver,然后再使用SimpleSoapExceptionResolver。
步骤1.在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(&#34; Ops !!!&#34;); //使用其他消息
它对我有用,我得到了以下内容:
<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 !!!
此致