File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes Returning custom Objects from Web Service Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Returning custom Objects from Web Service" Watch "Returning custom Objects from Web Service" New topic
Author

Returning custom Objects from Web Service

deepak kukreja
Ranch Hand

Joined: Aug 13, 2001
Posts: 54
Hi - I want my web service to have methods which return Custom objects e.g. Employee, Project etc objects. I am using Axis2 with the ADB data binding. I used wsdl2java to create the stubs and call the stub method from the client program.

WSDL has the following type for Employee

<xs:element minOccurs="0" name="return" nillable="true" type="ax22:Employee"/>

Now when I try to invoke a method from the client progrom, I get the following exception:

org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Any type element type has not been given

Can you suggest the changes required on client/server side?

Thanks.


Deepak<br />SCJP
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
"dnewco",

Please check your private messages regarding an important administrative matter.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
dnewco wrote:org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Any type element type has not been given.


This suggests that the WSDL specified a {http://www.w3.org/2001/XMLSchema}anyType parameter for the operation. This means that it is expecting any wellformed XML element. The stub may specify java.lang.Object, but it's seems to be looking for an object that contains the object representation of the raw XML - not a Java custom object.
deepak kukreja
Ranch Hand

Joined: Aug 13, 2001
Posts: 54
Do you mean I need to create XML representation in the web service for all these objects and return it to the client as a String? Will appreciate if you can point me to any sample code if available.

Thanks
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
I don't know what you have done so far.

deepak kukreja wrote:I want my web service to have methods which return Custom objects e.g. Employee, Project etc objects. I am using Axis2 with the ADB data binding.

This suggests that you created the web service yourself. Did you use Java2WSDL to do that? I suspect that you had some Java methods that simply returned java.lang.Objects which is how you ended up with the {http://www.w3.org/2001/XMLSchema}anyType. That approach does not work the way that you hoped it would.

I used wsdl2java to create the stubs and call the stub method from the client program.

This suggests that there is already a WSDL in existence (possibly from Java2WSDL) - you are just creating consumer stubs. This suggests that you are accessing an already existing web service - not creating one.

One thing that you have to understand is that the premise of web service interoperability is based on the fact that provider and consumer are exchanging semi-structured data in XML format - not as platform dependent object representations (e.g. Java Objects). So when you use Java2WSDL and then WSDL2Java you will probably not get the Java Objects on the consumer side that you had on the provider side. The objects on the consumer side are merely "data containers" that contain the same data that was in the XML that went over the wire.

That is why Contract first is considered a "Best Practice" in web service development. (See also the second half of this post)
  • Develop an XML Schema that contains EmployeeType, ProjectType, etc. element definitions that hold the equivalent data of your Employee, Project objects
  • Use that XML Schema in the definition of you web service contract (WSDL)
  • Use WSDL2Java to generate a service skeleton. This will also generate the ADB versions of EmployeeType, ProjectType etc.
  • In your glue code between the service logic and the service skeleton code, put your Employee to EmployeeType (and back), etc. conversions
  • Use WSDL2Java to generate a consumer stub. Remember the consumer may not even be interested in your Employee and Project Objects, they may have their own. So typically one would write a wrapper around the consumer stub which would be responsible for the EmployeeType to Employee (and back) conversions. This basically relates to the Business Delegate and Remote Fa├žade pattern.

  • With this approach you should be able to avoid any manual handling of XML in your code - however you need to deal with XML in terms of the XML Schema and WSDL.
    Pieter Bosman
    Greenhorn

    Joined: Jun 17, 2009
    Posts: 3
    I stumbled on this thread as I had the same issues as Deepak had.

    @Peer:
    I sincerely hope you don't take this the wrong way, but your reply, while 100% correct is, in my very humble opinion, completely beside the point of Deepak's question. He asks for help on something he's working on, so help him with his issue, don't tell him he's doing it wrong.
    Although I do realize his method of posting his question might have been a bit weird And on top of that it's late here in Belgium, so I might be cranky (sorry)


    @Deepak:
    http://www.ibm.com/developerworks/webservices/library/ws-tip-coding.html
    read it

    I got the exact same error message as you had. Analyzing the wsdl file that was generated by my Axis2 plugin in Eclipse, I noticed that the 'anyType' was used as a return type whenever I was exposing a method that was returning a 'List<MyObject>' type.
    So I modified those methods to return 'MyObject[]' types. Regenerated the webservice (and the wsdl) and behold, no more error.

    Hope it helped.

    Pieter


    When all else fails, check for CKI-problems.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Returning custom Objects from Web Service