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

kamaci
  • 72,915
  • 69
  • 228
  • 366

1 Answers1

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.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 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? – kamaci May 28 '12 at 21:10
  • @karmaci: Yes, with [tag:jaxws] you can annotate your exception with `@WebFault`. – Tomasz Nurkiewicz May 28 '12 at 21:11
  • There is something here: http://stackoverflow.com/questions/2064447/jax-ws-map-exceptions-to-faults but I don't know how to implement it. I need a simple basic example. – kamaci May 28 '12 at 21:11
  • I have implemented an example however my example doesn't work without defining targetNamespace element? Do you have an idea? – kamaci Aug 02 '12 at 16:51