Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Returning custom Objects from Web Service

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"dnewco",

Please check your private messages regarding an important administrative matter.
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic