Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » Throwing an exception from a web service
Throwing an exception from a web service [message #204650] Sat, 17 November 2007 19:19 Go to next message
Eclipse UserFriend
Originally posted by: not.me.telus.net

Hi, all

I'm just starting to learn about web services. From what I've read, it seems
like it's possible to throw an exception in a web service function and have
"the same" exception caught in the WS client.

I have tried to do this with Eclipse Europa and Axis2, but without success.
The WSDL generated from my WS class includes info about a fault, and the
Eclipse WS client wizard creates an exception class, but when I throw the
exception in the server I get an AxisFault on the client side, which doesn't
contain any of the info I put into my exception.

Is there any documentation or example code out there on this? If not, can
anyone give me some guidelines?

Thanks!

- rick
Re: Throwing an exception from a web service [message #204695 is a reply to message #204650] Mon, 19 November 2007 08:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mauro.molinari.cardinis.com

rick cameron ha scritto:
> Hi, all
>
> I'm just starting to learn about web services. From what I've read, it seems
> like it's possible to throw an exception in a web service function and have
> "the same" exception caught in the WS client.
>
> I have tried to do this with Eclipse Europa and Axis2, but without success.
> The WSDL generated from my WS class includes info about a fault, and the
> Eclipse WS client wizard creates an exception class, but when I throw the
> exception in the server I get an AxisFault on the client side, which doesn't
> contain any of the info I put into my exception.
>
> Is there any documentation or example code out there on this? If not, can
> anyone give me some guidelines?
>
> Thanks!
>
> - rick

Hi Ricky,
I too experienced the same problem. It also seems that WSDL generated by
Java2WSDL has some validation errors (please see:
http://issues.apache.org/jira/browse/AXIS2-3301).

From my own experience, it is FAR better to follow the top-down
approach to write new web services, because the "out-of-the-box"
experience promised by Java2WSDL is not honoured, except for very simple
"HelloWorld" cases.

Cheers,
Mauro.
Re: Throwing an exception from a web service [message #205230 is a reply to message #204695] Fri, 30 November 2007 23:45 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: not.me.telus.net

Ciao, Mauro

I have managed to get exceptions to work in the bottom-up scenario. It
helped to use that latest Eclipse (3.3.1.1) and Axis2 (1.3).

Declare an exception class that derives from AxisFault and exposes one or
more getters - e.g.

package wtp;

public class ConverterException extends AxisFault {

private final int code;



public ConverterException(String message, int code) {

super(message);

this.code = code;

}



public String getMessage() {

return super.getMessage();

}



public int getCode() {

return code;

}

}



The WSDL auto-generated by Axis2 will include the two preperties, and the
web service client code generated will be able to parse them from the SOAP
envelope. However, when the service throws the exception, the SOAP envelope
will not contain anything special, so the client will just see an AxisFault.



However, if you change the constructor as follows:



public ConverterException(String message, int code) {

super(message);



this.code = code;



SOAPFactory soapFactory =
OMAbstractFactory.getSOAP12Factory();



SOAPFaultDetail soapFaultDetail =
soapFactory.createSOAPFaultDetail();

QName qName = new QName("http://wtp",
"ConverterException");

OMElement detail =
soapFactory.createOMElement(qName, soapFaultDetail);



OMElement exception =
soapFactory.createOMElement(qName, null);

detail.addChild(exception);



qName = new QName("http://wtp/xsd", "code");

OMElement codeElement =
soapFactory.createOMElement(qName, null);

codeElement.setText(ConverterUtil.convertToString(code));

exception.addChild(codeElement);



qName = new
javax.xml.namespace.QName("http://wtp/xsd", "message");

OMElement messageElement =
soapFactory.createOMElement(qName, null);

messageElement.setText(ConverterUtil.convertToString(getMess age()));

exception.addChild(messageElement);



setDetail(detail);

}



....magic happens on the client side: it will get an exception called
something like ConverterExceptionException0, and the message and code can be
dug out as follows:



} catch (ConverterExceptionException0 e) {

ConverterException1 faultMessage =
e.getFaultMessage();



ConverterException converterException =
faultMessage.getConverterException();



System.out.println ("Conversion error:
code " + converterException.getCode() + ", " +
converterException.getMessage());

}



It seems that the names of the classes ConverterExceptionException0 and
ConverterException1 can vary.



It works, but there are some odd things - like, why are the code and message
in the namespace http://wtp/xsd, not http://wtp ?



Cheers



- rick


"Mauro Molinari" <mauro.molinari@cardinis.com> wrote in message
news:fhriqp$c9o$1@build.eclipse.org...
> rick cameron ha scritto:
>> Hi, all
>>
>> I'm just starting to learn about web services. From what I've read, it
>> seems
>> like it's possible to throw an exception in a web service function and
>> have
>> "the same" exception caught in the WS client.
>>
>> I have tried to do this with Eclipse Europa and Axis2, but without
>> success.
>> The WSDL generated from my WS class includes info about a fault, and the
>> Eclipse WS client wizard creates an exception class, but when I throw the
>> exception in the server I get an AxisFault on the client side, which
>> doesn't
>> contain any of the info I put into my exception.
>>
>> Is there any documentation or example code out there on this? If not, can
>> anyone give me some guidelines?
>>
>> Thanks!
>>
>> - rick
>
> Hi Ricky,
> I too experienced the same problem. It also seems that WSDL generated by
> Java2WSDL has some validation errors (please see:
> http://issues.apache.org/jira/browse/AXIS2-3301).
>
> From my own experience, it is FAR better to follow the top-down approach
> to write new web services, because the "out-of-the-box" experience
> promised by Java2WSDL is not honoured, except for very simple "HelloWorld"
> cases.
>
> Cheers,
> Mauro.
Re: Throwing an exception from a web service [message #205337 is a reply to message #205230] Mon, 03 December 2007 11:31 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mauro.molinari.cardinis.com

rick cameron ha scritto:
> Ciao, Mauro
>
> I have managed to get exceptions to work in the bottom-up scenario. It
> helped to use that latest Eclipse (3.3.1.1) and Axis2 (1.3).
>
> Declare an exception class that derives from AxisFault and exposes one or
> more getters - e.g.

Hi Rick,
your results are interesting, thank you!
However, the fact that you have to extend AxisFault is not so "elegant",
because you're bounding yourself to Axis2 (what if you'd like to use
another SOAP engine in the future?). It would be better if one could use
really custom exceptions.

I don't know if this can help (I didn't try, as I abandoned the
bottom-up approach in favour of the top-down one), but when you start
from the WSDL and declare some faults, WSDL2Java generates custom
exceptions with a special "fault message" field:

public CustomException extends Exception
{
<your-fault-type-in-WSDL> faultMessage

[... default constructors from Exception]

public void setFaultMessage(<your-fault-type-in-WSDL> msg)
{
this.faultMessage = msg;
}

public <your-fault-type-in-WSDL> getFaultMessage()
{
return this.faultMessage;
}
}

So, maybe you could go some further steps ahaed trying to write
exceptions with a property called "faultMessage" and corresponding
getter/setter, to use them in your Java code and to see whether
Java2WSDL treats them in any special way...

Cheers,
Mauro.
Re: Throwing an exception from a web service [message #205362 is a reply to message #205337] Tue, 04 December 2007 05:06 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: not.me.telus.net

"Mauro Molinari" <mauro.molinari@cardinis.com> wrote in message
news:fj0pfo$n48$1@build.eclipse.org...
> rick cameron ha scritto:
>> Ciao, Mauro
>>
>> I have managed to get exceptions to work in the bottom-up scenario. It
>> helped to use that latest Eclipse (3.3.1.1) and Axis2 (1.3).
>>
>> Declare an exception class that derives from AxisFault and exposes one or
>> more getters - e.g.
>
> Hi Rick,
> your results are interesting, thank you!
> However, the fact that you have to extend AxisFault is not so "elegant",
> because you're bounding yourself to Axis2 (what if you'd like to use
> another SOAP engine in the future?). It would be better if one could use
> really custom exceptions.
>
> I don't know if this can help (I didn't try, as I abandoned the bottom-up
> approach in favour of the top-down one), but when you start from the WSDL
> and declare some faults, WSDL2Java generates custom exceptions with a
> special "fault message" field:
>
> public CustomException extends Exception
> {
> <your-fault-type-in-WSDL> faultMessage
>
> [... default constructors from Exception]
>
> public void setFaultMessage(<your-fault-type-in-WSDL> msg)
> {
> this.faultMessage = msg;
> }
>
> public <your-fault-type-in-WSDL> getFaultMessage()
> {
> return this.faultMessage;
> }
> }
>
> So, maybe you could go some further steps ahaed trying to write exceptions
> with a property called "faultMessage" and corresponding getter/setter, to
> use them in your Java code and to see whether Java2WSDL treats them in any
> special way...
>
> Cheers,
> Mauro.

Ciao, Mauro

I didn't try making my exception extend, say, Exception rather than
AxisFault because of a comment I read that suggested that it should extend
AxisFault - but I will try this is I can find time!

Your suggestion of including a field called faultMessage is quite
interesting. As you saw in my code sample, that's what the exception class
looks like on the client side!

I don't need to create a more complex structure for my exception class; I
just need a String and an int, so my current code suits my needs.

A pi
Re: Throwing an exception from a web service [message #634166 is a reply to message #205362] Wed, 20 October 2010 16:29 Go to previous message
M4nux  is currently offline M4nux Friend
Messages: 1
Registered: October 2010
Junior Member
Hello guys,
So ? Is there any proper solution for this problem ? (without extends AxisFault on the server side)
I've tried to put an attribute named faultMessage on my custom exception but it still not works...
I definitly feel stuck...
Thanks
Previous Topic:[SOLVED] Attribute 'attrname' is not allowed to appear in element 'elem'
Next Topic: How to generate a *.wsdl from Java class?
Goto Forum:
  


Current Time: Sun Dec 21 10:08:51 GMT 2014

Powered by FUDForum. Page generated in 0.01784 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software