You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Josef Bajada <Jo...@go.com.mt> on 2008/11/02 12:43:06 UTC

Throwing custom exception

I am using CXF on Tomcat 6.0.18 and have created my webservice using
JAX-WS annotations. I need to throw my own exception from this
webservice because I need to have custom fields in it.

However, in the WSDL being created by CXF my custom exception is not
including the extra fields. What am I doing wrong?

My exception looks something like this:

 

public class MyException extends Exception

{

    int faultCode;

 

    public MyException(String desc, int faultCode)

    {

         super (desc);

         this.faultCode = faultCode;

     }

 

     public int getFaultCode()

     {

        return faultCode;

     }

}

 

 

My Webservice looks like this:

 

@WebService

public class MyWebservice

{

   @WebMethod

   public void myMethod()

           throws MyException 

}

 

Isnt this the proper way to throw an exception with JAX-WS? Is CXF doing
something it shouldn't or am I missing something?

 

Thanks,

 

Josef


RE: Throwing custom exception

Posted by Josef Bajada <Jo...@go.com.mt>.
I think I understood better what you suggested with your second option
and it makes sense (and also explains why it worked for me when I
changed the error code to public).

Since I didn't have a setter method and the field was private CXF can't
repopulate it on the client's side when it receives it, so that is
probably why the field was missing. I will try add the setter method to
the custom field and see what happens.

Regards,

Josef


-----Original Message-----
From: Josef Bajada [mailto:Josef.Bajada@go.com.mt] 
Sent: 03 November 2008 18:36
To: Daniel Kulp; users@cxf.apache.org
Subject: RE: Throwing custom exception

Hi Daniel,

Well when I looked around it seems that using @WebFault in your code is
not something you should do, at lease according to the JAX-WS annotation
documentation
(https://jax-ws.dev.java.net/jax-ws-ea3/docs/annotations.html#3.6%20java
x.xml.ws.WebFault%7Coutline)

I was already including the custom information (such as the errorCode)
in the constructor and a getter method, but it was still excluding it
from the WSDL.

On the other hand, I noticed that if I do it like this (the field is set
to public), it suddenly works:

public MyException extends Exception
{
	public int errorCode;

      public MyException(String desc, int errorCode)
	{
		super(desc);
		this.errorCode = errorCode;
	}

   	public int getErrorCode()
	{
		return errorCode;
	}
}

Why should I put a member field public to have it present in the WSDL?
It is not a requirement for objects which are return types instead of
exceptions and they are still reflected correctly in the WSDL.

Thanks,

Josef


-----Original Message-----
From: Daniel Kulp [mailto:dkulp@apache.org] 
Sent: 03 November 2008 18:25
To: users@cxf.apache.org
Cc: Josef Bajada
Subject: Re: Throwing custom exception



I think you have two options:

1) Make the exception look like a normal JAX-WS generated exception.
That 
would basically mean defining a JAXB bean for the data that has all the 
getter/setters and such for the data and making the exception look like:

@WebFault
public class MyException extends Exception {
     MyFaultInfo faultInfo;
     public MyException(String msg, MyFaultInfo mfi) {
            ...
     }
     public MyFaultInfo getFaultInfo() {
         return faultInfo;
     }
}
That is basically the "JAX-WS" standard way of doing it and would be
portable.   
The entire schema for the exception is defined in the JAXB MyFaultInfo
bean.


2)  For CXF, you can add a setter for your extra data and a constructor
of 
just:

 public MyException(String msg) {...}
I think CXF will then call that constructor (maybe also need a no-args,
but I 
think the single arg works) and then call the setters for anything it
finds.

Dan




On Sunday 02 November 2008 6:43:06 am Josef Bajada wrote:
> I am using CXF on Tomcat 6.0.18 and have created my webservice using
> JAX-WS annotations. I need to throw my own exception from this
> webservice because I need to have custom fields in it.
>
> However, in the WSDL being created by CXF my custom exception is not
> including the extra fields. What am I doing wrong?
>
> My exception looks something like this:
>
>
>
> public class MyException extends Exception
>
> {
>
>     int faultCode;
>
>
>
>     public MyException(String desc, int faultCode)
>
>     {
>
>          super (desc);
>
>          this.faultCode = faultCode;
>
>      }
>
>
>
>      public int getFaultCode()
>
>      {
>
>         return faultCode;
>
>      }
>
> }
>
>
>
>
>
> My Webservice looks like this:
>
>
>
> @WebService
>
> public class MyWebservice
>
> {
>
>    @WebMethod
>
>    public void myMethod()
>
>            throws MyException
>
> }
>
>
>
> Isnt this the proper way to throw an exception with JAX-WS? Is CXF
doing
> something it shouldn't or am I missing something?
>
>
>
> Thanks,
>
>
>
> Josef



-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog

RE: Throwing custom exception

Posted by Josef Bajada <Jo...@go.com.mt>.
Hi Daniel,

Well when I looked around it seems that using @WebFault in your code is
not something you should do, at lease according to the JAX-WS annotation
documentation
(https://jax-ws.dev.java.net/jax-ws-ea3/docs/annotations.html#3.6%20java
x.xml.ws.WebFault%7Coutline)

I was already including the custom information (such as the errorCode)
in the constructor and a getter method, but it was still excluding it
from the WSDL.

On the other hand, I noticed that if I do it like this (the field is set
to public), it suddenly works:

public MyException extends Exception
{
	public int errorCode;

      public MyException(String desc, int errorCode)
	{
		super(desc);
		this.errorCode = errorCode;
	}

   	public int getErrorCode()
	{
		return errorCode;
	}
}

Why should I put a member field public to have it present in the WSDL?
It is not a requirement for objects which are return types instead of
exceptions and they are still reflected correctly in the WSDL.

Thanks,

Josef


-----Original Message-----
From: Daniel Kulp [mailto:dkulp@apache.org] 
Sent: 03 November 2008 18:25
To: users@cxf.apache.org
Cc: Josef Bajada
Subject: Re: Throwing custom exception



I think you have two options:

1) Make the exception look like a normal JAX-WS generated exception.
That 
would basically mean defining a JAXB bean for the data that has all the 
getter/setters and such for the data and making the exception look like:

@WebFault
public class MyException extends Exception {
     MyFaultInfo faultInfo;
     public MyException(String msg, MyFaultInfo mfi) {
            ...
     }
     public MyFaultInfo getFaultInfo() {
         return faultInfo;
     }
}
That is basically the "JAX-WS" standard way of doing it and would be
portable.   
The entire schema for the exception is defined in the JAXB MyFaultInfo
bean.


2)  For CXF, you can add a setter for your extra data and a constructor
of 
just:

 public MyException(String msg) {...}
I think CXF will then call that constructor (maybe also need a no-args,
but I 
think the single arg works) and then call the setters for anything it
finds.

Dan




On Sunday 02 November 2008 6:43:06 am Josef Bajada wrote:
> I am using CXF on Tomcat 6.0.18 and have created my webservice using
> JAX-WS annotations. I need to throw my own exception from this
> webservice because I need to have custom fields in it.
>
> However, in the WSDL being created by CXF my custom exception is not
> including the extra fields. What am I doing wrong?
>
> My exception looks something like this:
>
>
>
> public class MyException extends Exception
>
> {
>
>     int faultCode;
>
>
>
>     public MyException(String desc, int faultCode)
>
>     {
>
>          super (desc);
>
>          this.faultCode = faultCode;
>
>      }
>
>
>
>      public int getFaultCode()
>
>      {
>
>         return faultCode;
>
>      }
>
> }
>
>
>
>
>
> My Webservice looks like this:
>
>
>
> @WebService
>
> public class MyWebservice
>
> {
>
>    @WebMethod
>
>    public void myMethod()
>
>            throws MyException
>
> }
>
>
>
> Isnt this the proper way to throw an exception with JAX-WS? Is CXF
doing
> something it shouldn't or am I missing something?
>
>
>
> Thanks,
>
>
>
> Josef



-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog

Re: Throwing custom exception

Posted by Daniel Kulp <dk...@apache.org>.

I think you have two options:

1) Make the exception look like a normal JAX-WS generated exception.   That 
would basically mean defining a JAXB bean for the data that has all the 
getter/setters and such for the data and making the exception look like:

@WebFault
public class MyException extends Exception {
     MyFaultInfo faultInfo;
     public MyException(String msg, MyFaultInfo mfi) {
            ...
     }
     public MyFaultInfo getFaultInfo() {
         return faultInfo;
     }
}
That is basically the "JAX-WS" standard way of doing it and would be portable.   
The entire schema for the exception is defined in the JAXB MyFaultInfo bean.


2)  For CXF, you can add a setter for your extra data and a constructor of 
just:

 public MyException(String msg) {...}
I think CXF will then call that constructor (maybe also need a no-args, but I 
think the single arg works) and then call the setters for anything it finds.

Dan




On Sunday 02 November 2008 6:43:06 am Josef Bajada wrote:
> I am using CXF on Tomcat 6.0.18 and have created my webservice using
> JAX-WS annotations. I need to throw my own exception from this
> webservice because I need to have custom fields in it.
>
> However, in the WSDL being created by CXF my custom exception is not
> including the extra fields. What am I doing wrong?
>
> My exception looks something like this:
>
>
>
> public class MyException extends Exception
>
> {
>
>     int faultCode;
>
>
>
>     public MyException(String desc, int faultCode)
>
>     {
>
>          super (desc);
>
>          this.faultCode = faultCode;
>
>      }
>
>
>
>      public int getFaultCode()
>
>      {
>
>         return faultCode;
>
>      }
>
> }
>
>
>
>
>
> My Webservice looks like this:
>
>
>
> @WebService
>
> public class MyWebservice
>
> {
>
>    @WebMethod
>
>    public void myMethod()
>
>            throws MyException
>
> }
>
>
>
> Isnt this the proper way to throw an exception with JAX-WS? Is CXF doing
> something it shouldn't or am I missing something?
>
>
>
> Thanks,
>
>
>
> Josef



-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog