Spring-WS - 在Exception上返回有效响应

时间:2018-04-12 09:09:13

标签: spring exception-handling spring-ws

我有soap和point,它应该根据请求类型B返回响应类型A.但是在处理请求期间,我期待错误(比如无法调用downastream服务),例如类型ExpEx会抛出cutom异常。现在我想做自定义错误映射,因为如果出现错误我不想返回类型A但想要返回类型CFault(也在wsd中定义)。 现在问题: - 是可以创建自定义的eero句柄,而不是A或 - 或者是否有可能使enpoint允许返回两种类型的响应A和CFault(我认为对象)?

我的观点:

public class FantasticEndpoint extend WebServiceEndpoint {
private static final String NAMESPACE = "http://www.fantastic.com/SOA/tmp/FantasticService/v_2_4";

@PayloadRoot(namespace = NAMESPACE, localPart = "handleBOperation")
@ResponsePayload
public A createConsumers(@RequestPayload B b{
    //do some dangerous logic possility throw EXCEPTION
        // if EXCEPTION return CFault or return A if normal processing
}

}

1 个答案:

答案 0 :(得分:2)

首先,看一下:https://docs.spring.io/spring-ws/sites/2.0/reference/html/server.html#server-endpoint-exception-resolver

我将重点介绍其中的一些内容:

根据文档,您可以创建自己的异常类,以指示抛出异常时应返回的SOAP错误。只需使用@SoapFault注释注释类。

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

@SoapFault(faultCode = FaultCode.SERVER)
public class MyCustomException extends Exception {
    public MyClientException(String message) {
        super(message);
    }
}

如果这不适合你,你可以搞乱SoapFaultAnnotationExceptionResolver(https://docs.spring.io/spring-ws/site/apidocs/org/springframework/ws/soap/server/endpoint/SoapFaultAnnotationExceptionResolver.html)。此解析程序允许您将异常类映射到SOAP Fault:

<beans>
    <bean id="exceptionResolver" class="org.springframework.ws.soap.server.endpoint.SoapFaultMappingExceptionResolver">
    <property name="defaultFault" value="SERVER"/>
    <property name="exceptionMappings">
        <value>
            org.springframework.oxm.ValidationFailureException=CLIENT,Invalid request
        </value>
    </property>
</bean>

您可以使用它来添加SoapFaultDetail:

public class MySoapFaultDefinitionExceptionResolver extends SoapFaultMappingExceptionResolver {

private static final QName CODE = new QName("code");
private static final QName DESCRIPTION = new QName("description");

@Override
protected void customizeFault(Object endpoint, Exception ex, SoapFault fault) {
    if (ex instanceof MyCustomException) {
        SoapFaultDetail detail = fault.addFaultDetail();
        detail.addFaultDetailElement(CODE).addText("SOMECODE");
        detail.addFaultDetailElement(DESCRIPTION).addText(ex.getMessage());
    }
}

我曾经使用过EndpointInterceptor来搞乱SOAPHeader。也许您可以使用它来使用SOAPFault(https://docs.spring.io/spring-ws/site/apidocs/org/springframework/ws/server/EndpointInterceptor.html)。

您可以从MessageContext中提取SOAPFault,如下所示:

@Override
public boolean handleFault(MessageContext messageContext, Object o) throws Exception {
    SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext.getResponse();
    SOAPMessage soapMessage = soapResponse.getSaajMessage();
    SOAPBody body = soapMessage.getSOAPBody();
    SOAPFault fault = body.getFault();
    //do something with fault here
    return true
}

您可以在此处阅读有关SOAPFault界面https://docs.spring.io/spring-ws/site/apidocs/org/springframework/ws/soap/SoapFault.html

的信息