Results 1 to 7 of 7
  1. #1
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default Returning complex data types from a web method

    Hi

    I want to return from my web service a complex data type instead of a string. For example I want to return a person or more with name, surname and age. What are the best practices to do this? Is there some kind of standard that I should look at? Im using javax.jws as a framework.

    Thanks
    Tshegofatso Manakana
    a.k.a Untouchable

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    It's usual to serialise objects as xml when working with web applications. Something like
    Java Code:
    <message>
        <person name="foo" surname="bar" age="42" />
    </message>
    or
    Java Code:
    <message>
        <person>
            <name>foo</name>
            <surname>bar</name>
            <age>42</age>
        </person>
    </message>
    There many alternatives to xml though.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    Ok
    Thanks for the reply. How does one go about implementing this? Do I create an XML object and return it instead? What would be the return data type of my method?
    Tshegofatso Manakana
    a.k.a Untouchable

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    The xml message should be stored and transmitted as plaintext, i.e. a String. Various methods can be used to create and parse xml, there's some code in java.beans, javax.xml, org.w3c.dom and org.xml.sax to get you started.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    If you're creating a web service, then you ought to be writing a WSDL which describes the service.

    w3schools has a tutorial on it...let's see, can I link?

    Phooey...too few posts.

    www w3schools com/wsdl/default.asp

    This should always be your start point for a web service...you really shouldn't start with code and then go "let's make this a service".

  6. #6
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    This should always be your start point for a web service...you really shouldn't start with code and then go "let's make this a service".
    What is wrong with this approach?
    Tshegofatso Manakana
    a.k.a Untouchable

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Quote Originally Posted by Tshegofatsom View Post
    What is wrong with this approach?
    A web service should be implementation neutral. That is, it shouldn't care about what you have behind it (.NET, Java, something else). It shouldn't only care about what service it provides, ie what it sends back to the caller and what it needs to do its job.

    If you start from the implementation and auto-generate the WSDL you run the risk of unwittingly tieing the service to, say, your Java implementation. So it is good practice to start from the WSDL, defining the service in that way.

    The code should be coupled to the service and not vice versa.

Similar Threads

  1. Returning data from a JFrame/JDialog?!
    By Joe2003 in forum AWT / Swing
    Replies: 6
    Last Post: 01-08-2009, 01:14 AM
  2. Need help. Method won't returning proper value..
    By zlwilly in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 10:44 PM
  3. Array of different data types?
    By venkatteshb in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 06:42 PM
  4. Replies: 0
    Last Post: 03-11-2008, 05:55 PM
  5. Converting data types
    By bluekswing in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 01:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •