如何使用Spring-WS将异常转换为返回代码?

Axe*_*ine 8 java soap web-services spring-ws jaxb2

我目前正面临使用Spring WS的错误代码和消息的问题.

我们使用带有JAXB2绑定的Spring WS 2.0以及@Endpoint和@PayloadRoot注释以方便使用.

我们的端点如下所示:

@Endpoint
public class MyEndpoint() {
    private static final String MY_NAMESPACE=...;

    @PayloadRoot(namespace=MY_NAMESPACE, localPart="myPart")
    public MyPartResponse handleMyPart(MyPart myPart) {
        ....
    }
}
Run Code Online (Sandbox Code Playgroud)

我们仅将soap用作围绕由XSD定义的POX消息的薄包装器.这也意味着我们使用返回码和消息而不是故障.

每个响应都继承自

<xs:complexType name="ResultBase">
    <xs:sequence>
        <xs:element name="errorCode" type="tns:ErrorCode" />
        <xs:element name="errorMessage" type="xs:string" />
    </xs:sequence>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)

并在成功的情况下添加一些细节,如下所示:

<xs:element name="MySpecificResponse">
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="tns:ResultBase">
                <xs:sequence>
                    <xs:element name="mySpecificElement" type="tns:MySpecificType" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)

可以干净地映射handleMyPart方法中抛出的所有异常.

但是,有两种类型的错误仍然未被捕获并生成错误而不是明确的错误消息:

  • XSD验证错误
  • 格式错误的XML错误

在一天结束时,这些是与使用Spring WS的每个POX Web服务相关的问题.如何拦截这些异常并将其映射到响应对象?

但请记住:所有响应对象都略有不同,因为它们都是从公共响应对象继承的,但是为它添加了一些唯一的可选内容.

lre*_*der 5

一种对我有用的方法是:

对于XSD验证错误,请扩展AbstractValidatingInterceptor以提供XSD验证错误的自定义处理,并将其设置为Spring上下文中的validatingInterceptor bean.

对于格式错误的XML,请扩展MessageDispatcherServlet.覆盖doService以捕获DomPoxMessageException,并在捕获该异常时添加自己的自定义处理.将自定义的MessageDispatcherServlet设置为web.xml中的spring-ws servlet.

我在博客文章中写了这篇文章,其中包含了令人作呕的细节:

http://www.dev-garden.org/2011/09/03/handling-pox-errors-in-spring-ws-part-1/

-Larry