vinoth subramaniam wrote:How can i pass them ?
You've got bigger problems. Not only are you trying to return "A list of books" - you are trying to return an entire object graph and to make things worse, an object graph with cycles. XML is notoriously bad at representing graphs (and the cycles don't help either) because XML is hierarchical in nature - so it is necessary to restructure the information to make it more XML-friendly. In most cases the "listed" object is simply stripped down to its bare essentials so that no object-graph remains.
If it is necessary to retain
all the information in the object-graph then a different approach is required. The XML document has to represent
all the instances of any type that appears in the
entire graph and any relationships are realized through
{http://www.w3.org/2001/XMLSchema}IDREF and
{http://www.w3.org/2001/XMLSchema}ID types. So in the case of the Book/Author example the document needs three sub-sequences:
a books sequence. This sequence contains all the books in the graph - not just the ones that belong to the List<Book>. Each book can reference multiple authors.an authors sequence. This sequence contains all the authors in the graph. Each author can reference multiple books.a list sequence. This sequence simply contains references to the books that are actually in the List<Book>.
The
{http://idref.jaxws.coderanch.com/}bookList in the following schema realizes this structure
The {http://idref.jaxws.coderanch.com/}BookRef uses an {http://www.w3.org/2001/XMLSchema}IDREF to refer to the isbn13 of a {http://idref.jaxws.coderanch.com/}Book. It is used by the list to identify the books in "the list" and by {http://idref.jaxws.coderanch.com/}Author to identify the author's books.The {http://idref.jaxws.coderanch.com/}AuthorRef uses an {http://www.w3.org/2001/XMLSchema}IDREF to refer to the id of an {http://idref.jaxws.coderanch.com/}Author. It is used by {http://idref.jaxws.coderanch.com/}Book to identify a book's authors.
This schema can now be used in a web service contract
To keep things simple I'm going to proceed with JDK 6 tools only (i.e. JAX-WS & JAXB - which I believe are supported by
JBoss 5).
A JAX-WS binding file
which maps the
Java web service artifacts to the
com.coderanch.jaxws.idref.ws package.
A JAXB external binding customization file
which maps the JAXB artifacts to the
com.coderanch.jaxws.idref.dto package. The main reason for using the customization is to force strong typing on the IDREF links and to suppress the use of JAXBElement wrappers. Of course many of the names for the Java classes are customized as well.
wsimport BookSearch.wsdl -b BookSearch.xml -b BookSearch.xjb -s src
is then used to generate the Java artifacts:
com.coderanch.jaxws.idref.ws.BookSearch - the endpoint interfacecom.coderanch.jaxws.idref.ws.BookSearch_Service - the web service client proxycom.coderanch.jaxws.idref.dto.AuthorDto class to represent {http://idref.jaxws.coderanch.com/}Author.com.coderanch.jaxws.idref.dto.AuthorHolder class to represent {http://idref.jaxws.coderanch.com/}AuthorRef.com.coderanch.jaxws.idref.dto.BookDto class to represent {http://idref.jaxws.coderanch.com/}Book.com.coderanch.jaxws.idref.dto.BookHolder class to represent {http://idref.jaxws.coderanch.com/}BookRef.com.coderanch.jaxws.idref.dto.BookList class to represent {http://idref.jaxws.coderanch.com/}BookList.com.coderanch.jaxws.idref.dto.BookSearchRequest class to represent {http://idref.jaxws.coderanch.com/}BookSearchRequest.com.coderanch.jaxws.idref.dto.ObjectFactory - the JAXB ObjectFactory.package-info.java containing the JAXB package level annotations.
At this point the building blocks for the web service are in place. Of course some domain objects are necessary. The Book, Author, and the Repository class that returns them.
As you may notice the Repository returns a List<Book> while the generated service endpoint interface returns a
com.coderanch.jaxws.idref.dto.BookList, so for the conversion we need an adapter:
Finally we can put together the web service implementation.
And the code to host the endpoint
To compile everything:
javac -d . src\com\coderanch\jaxws\idref\domain\*.java
javac -d . src\com\coderanch\jaxws\idref\util\BookListAdapter.java
javac -d . src\com\coderanch\jaxws\idref\provider\*.java
wsgen -cp . com.coderanch.jaxws.idref.provider.BookSearchImpl
Start the web service with:
java com.coderanch.jaxws.idref.provider.Main
If you point a browser to
http://localhost:8080/wsdemo/booksearch?wsdl you should see the JAX-WS generated WSDL.
Open a separate console window to work on the web service consumer.
And compile it with
javac -d . src\com\coderanch\jaxws\idref\consumer\BookSearchClient.java
and run it with
java com.coderanch.jaxws.idref.consumer.BookSearchClient
The
SOAP request for the
{http://idref.jaxws.coderanch.com/}bookList would look like this:
Notice how there is no "search" method in the request. That is a characteristic of the document/literal messaging mode. An operation can only receive one parameter, the "input document". The type of the "input document" determines which method is executed. In this case the payload is of the
{http://idref.jaxws.coderanch.com/}BookSearchRequest type, so the payload is processed by the "search" WebMethod.
The response carries all the result information. The "list" holds the references to the books that are part of the result set; "books" holds all the books that are in the object graph, each book having one or more author references; "authors" holds all the authors that are in the object graph, each author having one or more book references.
(linebreaks and whitespace added, chunking lengths removed for readability)
An example of an empty
{http://idref.jaxws.coderanch.com/}bookList:
The "list", "books", and "authors" elements are simply "nil" elements.
So all this is basically an example of how a List<Book> is converted to a BookList on the provider side and then reconstituted to a List<Book> on the consumer side.