gpt4 book ai didi

spring - 在 WS SoapFault : my custom ExceptionResolver is not used 中添加细节

转载 作者:行者123 更新时间:2023-12-03 19:17:07 27 4
gpt4 key购买 nike

我正在使用 Spring Boot (1.2.4.RELEASE) 构建一个 Web 服务,我对这个框架很陌生。
特别是,我正在尝试在抛出异常时自定义 SoapFault 内容 (添加“详细信息”标签)。

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

这是我的异常(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;
}
}

这是我的 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 而不是我的......

有人看到解释吗?

已经看过了:
  • SoapFaultMappingExceptionResolver never gets hit with regular java exception
  • 最佳答案

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

    我试图覆盖的 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;
    }

    }

    关于spring - 在 WS SoapFault : my custom ExceptionResolver is not used 中添加细节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31069114/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com