Reputation: 75127
I can't throw an exception at my web service's method. When I throw an exception at my method and try to test my application from Soap UI it gives that error:
Failed to import WSDL
and
java.lang.NullPointerException
my question is that:
How to write an user defined exception for Java Web Services?
(i.e. there is a method that returns a student given by a name. However throwing an error if student can't find at database?)
Upvotes: 0
Views: 778
Reputation: 340713
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.
Upvotes: 1