我不能在我的 Web 服务的方法上抛出异常。当我在我的方法中抛出异常并尝试从 Soap UI 测试我的应用程序时,它会给出该错误:
Failed to import WSDL
和
java.lang.NullPointerException
我的问题是:
如何为 Java Web 服务编写用户定义的异常?
(即,有一种方法可以返回由名称给出的学生。但是,如果学生在数据库中找不到,则会引发错误?)
我不能在我的 Web 服务的方法上抛出异常。当我在我的方法中抛出异常并尝试从 Soap UI 测试我的应用程序时,它会给出该错误:
Failed to import WSDL
和
java.lang.NullPointerException
我的问题是:
如何为 Java Web 服务编写用户定义的异常?
(即,有一种方法可以返回由名称给出的学生。但是,如果学生在数据库中找不到,则会引发错误?)
In wsdl exceptions are first-class citizens and are known as soap faults. Basically a fault is yet another message that your operation can return - but with different semantics. Typically faults are translated to strongly-typed exceptions in endpoint interfaces.
Example taken from Web services hints and tips: Design reusable WSDL faults:
<message name="faultMsg"><part name="fault" element="tns:fault"/>
<portType name="Interface">
<operation name="op1">
<input name="op1Request" message="tns:op1RequestMsg"/>
<output name="op1Response" message="tns:op1ResponseMsg"/>
<fault name="fault" message="tns:faultMsg"/>
</operation>
</portType>
If you throw suvh exception from your service method it is translated to <fault>
message and rethrown on the client side. If you simply throw arbitrary message on the server side, it is treated as an error and might result e.g. with 500 error code on the client side.