0

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?)

1 Answer 1

1

In exceptions are first-class citizens and are known as s. 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.

Sign up to request clarification or add additional context in comments.

4 Comments

I am new to java web services. I use Spring and don't write any wsdl schema, it is generated automatically. Can I do that at Java side?
@karmaci: Yes, with jaxws you can annotate your exception with @WebFault.
There is something here: stackoverflow.com/questions/2064447/… but I don't know how to implement it. I need a simple basic example.
I have implemented an example however my example doesn't work without defining targetNamespace element? Do you have an idea?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.