File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Services and the fly likes Custom Exception in WebService Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Custom Exception in WebService" Watch "Custom Exception in WebService" New topic

Custom Exception in WebService

Zaid Ansari

Joined: Dec 29, 2003
Posts: 10
I want my WebService to throw Custom Defined Exception 'AuthorizationFailedException' like
ManageUserResponse ManageUser(@WebParam(name = "ManageUser") ManageUser body) throws AuthorizationFailedException;

the issue is i am using xsd Schema and i am not able to generate 'AuthorizationFailedException' class that extends java.lang.Exception.
how can i define type of any xsd elemet to Java.lang.Exception.

I need smething like
<xsd:element name="AuthorizationFailedException" type="java.lang.Exception"></xsd:element>

Zaid Ansari
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
If you want to map an exception to a fault bean, then take a look at the @WebFault annotation.
Otherwise, I am not entirely clear if you do Java first or WSDL first development?
Perhaps it is even meet-in-the-middle?
Have you tried using XJC, the JAXB tool, to generate Java bean classes from the XML schema?
Best wishes!
Zaid Ansari

Joined: Dec 29, 2003
Posts: 10
thanks for reply,
@WebFault annotation is part of WSDL rigth?
The issue is i am usign xFire Codehaus to generate my WSDL and schema classes. and i don't know what settign are required so generated WSDL contain @WebFault annotation.
I need help in generating WSDL with @WebFault.
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Can you post you Exception class code as most of the times the problem with such case is that either you have field defined private in the exception which is used in the constructor.

We would be able to understand your problem if you paste your Exception defination code here.


Rahul Juneja
ThoughtClicks -
Zaid Ansari

Joined: Dec 29, 2003
Posts: 10
Well i don't have any separate class there for exception, what i all did is that i created a complex type with name 'AuthorizationFailedException' and when xFire Generator generated its JAXB class i added 'extends Exception' to that. I know that it must be wrong but i am doing it first time so i have no such idea how we can use User Defined Exception. After that i added thorws AuthorizationFailedException in my WS methods. when i made request it showed error that illegalAnnotationExcetion and that no default Constructor found.

So I want to know that how can i make xsd that will generate a valid wsdl:fault in WSDL on generation. i will be grateful if someone provide us sample code of xsd schema that generates a valid wsdl:fault element. Or explain how we can use user defined exception in WS.
I agree. Here's the link:
subject: Custom Exception in WebService
jQuery in Action, 3rd edition