【问题标题】:Jax-ws ignores custom exceptionsJax-ws 忽略自定义异常
【发布时间】:2013-09-25 16:21:41
【问题描述】:

我的 Web 服务的某些方法应该向客户端返回自定义异常。但是 jax-ws 在发布端点时会忽略自定义异常的声明。
服务和方法的描述如下(该接口的实现有相同的注解):

@WebService(name = "ContainerProxy")
@SOAPBinding(style = Style.RPC)
public interface ContainerProxy {

    @WebMethod
    public Response processRequest(Request request) throws
        BadRequestException, // Custom exception
        ApplicationNotFoundException, // Custom exception
        ClassNotFoundException,
        NoSuchMethodException,
        IllegalAccessException,
        InvocationTargetException;
}

为此服务生成的 WSDL 如下所示:

<?xml...
    <portType name="ContainerProxyImpl">
        <operation name="processRequest">
            <input wsam:Action="http://proxy.container.hive.org/ContainerProxyImpl/processRequestRequest"
               message="tns:processRequest"></input>
            <output wsam:Action="http://proxy.container.hive.org/ContainerProxyImpl/processRequestResponse"
                message="tns:processRequestResponse"></output>
            <fault message="tns:ClassNotFoundException" name="ClassNotFoundException"
               wsam:Action="http://proxy.container.hive.org/ContainerProxyImpl/processRequest/Fault/ClassNotFoundException"></fault>
            <fault message="tns:NoSuchMethodException" name="NoSuchMethodException"
               wsam:Action="http://proxy.container.hive.org/ContainerProxyImpl/processRequest/Fault/NoSuchMethodException"></fault>
            <fault message="tns:IllegalAccessException" name="IllegalAccessException"
               wsam:Action="http://proxy.container.hive.org/ContainerProxyImpl/processRequest/Fault/IllegalAccessException"></fault>
            <fault message="tns:InvocationTargetException" name="InvocationTargetException"
               wsam:Action="http://proxy.container.hive.org/ContainerProxyImpl/processRequest/Fault/InvocationTargetException"></fault>
        </operation>
        ...
    </portType>
    ...
    <binding name="ContainerProxyImplPortBinding" type="tns:ContainerProxyImpl">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
        <operation name="processRequest">
            <soap:operation soapAction=""></soap:operation>
            <input>
                <soap:body use="literal"></soap:body>
            </input>
            <output>
                <soap:body use="literal"></soap:body>
            </output>
            <fault name="ClassNotFoundException">
                <soap:fault name="ClassNotFoundException" use="literal"></soap:fault>
            </fault>
            <fault name="NoSuchMethodException">
                <soap:fault name="NoSuchMethodException" use="literal"></soap:fault>
            </fault>
            <fault name="IllegalAccessException">
                <soap:fault name="IllegalAccessException" use="literal"></soap:fault>
            </fault>
            <fault name="InvocationTargetException">
                <soap:fault name="InvocationTargetException" use="literal"></soap:fault>
            </fault>
        </operation>
        ...

那么如何将自定义异常返回给客户端?

【问题讨论】:

    标签: java web-services exception wsdl jax-ws


    【解决方案1】:

    问题是因为自定义异常必须是类的扩展 Exception 或其他扩展 Exception 的类。但我不知道,我的自定义异常是 Throwable 的扩展。

    【讨论】:

      【解决方案2】:

      您是否使用以下内容注释了 Web Service 类:

      @WebService(serviceName = "CustomWsName")
      @SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
      public class WebServiceClass{
      
      @WebMethod
      ..method...
      
      
      }
      

      或者使用另一个 SOAPBinding?

      【讨论】:

      • 尝试在我的示例中使用 SOAPBinding 进行测试。通过这种方式,我可以看到包含在已发布 wsdl 中的自定义异常。
      • 感谢您的帮助。我已经找到了这个问题的解决方案。
      猜你喜欢
      • 1970-01-01
      • 2011-04-14
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 2018-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多