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 Sending requests in xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Sending requests in xml " Watch "Sending requests in xml " New topic
Author

Sending requests in xml

Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hello -

I am able to write web service client and server and they are all working fine.

What I have heard about web service is that the exchange of information is in xml. What does that mean? How do I send request in xml?
Should I send it in the form of a string? How should I do that?

My project is web service extensive. and all the request and response is in xml. This is the first time I am working with web services. Kindly help me understand.

Best,
Hari Priya
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Originally posted by Hari priya:

What I have heard about web service is that the exchange of information is in xml. What does that mean? How do I send request in xml?
Should I send it in the form of a string? How should I do that?

This means that the SOAP request and SOAP response will be in XML format, your web serivces tool(like Apache axis) will do this for you, so you don't need to send it as string or other formats. You have to just invoke the webservice operation and read back the response. If you want to see what kind of XML is being transmitted between client and server you can use TCP/IP sniffers or SOAPMonitor tool from Apache Axis.


Spritle Software Blogs
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Thanks Balaji for your response.

For my requirement, I am supposed to write a service which reads data from a database sends teh result as an xml document to the client.

The client has to parse the resulting xml to read teh data.

So, here, I will be generating an xml and will be sending it as a string or document. Is this equivalent to the SOAP response that you have explained me? If not how is this response string or document will be sent?

Is it possible to send the query result as a SOAP response itself or as a strign or as a document object?

Kindly help me as I got confused here between the soap response and this one.

Many thanks for taking time.
Hari Priya
Sonia Gun
Greenhorn

Joined: Oct 15, 2004
Posts: 10
Soap is a protocol to carry your data from server to client. The soap message will have two parts Header(optional) and body. The body of the soap message is nothing but your xml. Once this soap message is composed, you will send it as your request/response.
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hello -

Thank you for your response Sonia. I understand it much better now. But I have another question now.

This is the statement, that I use to to hold the response



should I set the return type of the service method to SOAPEnvelope right?
Don't I have to set the return type as in



Thanks again!
Dharamvir (DV) Punia
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Hi,
If you want to access the information sent back to you from the webservice then you definately want to get handle to response if not then don't worry about it.
e.g
when you invoke a service method, a object is returned, you can cast it to string if you think its returning a string back as shown below:

Does that help.
-DV
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hi -

Thanks for the reply Dharamvir. My question is - what shoud we set the return type to if my web service returns a SOAPEnvelope.

Hari Priya
Dharamvir (DV) Punia
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Hi Priya,
This is what you looking for:


I have never tried this but you can give a try:



Here is also link to the tutorial as well: SAAJ

Does this help.
-DV
[ October 27, 2004: Message edited by: Dharamvir (DV) Punia ]
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hi Dharamvir -

Thanks for trying to help me. I understand what you are saying. The return type is a SOAPEnvelope. So, do I need to set the return type to something before I invoke the call using call.setReturnType()

Thank you!
Hari Priya
nilesh Katakkar
Ranch Hand

Joined: Oct 27, 2004
Posts: 35
hi

Since your webservice is going to talk to database and return XML resultset to the client, you possibly can do this.

In the following example I am using Apache Axis toolkit. If you are using different toolkit this will give you an idea hopefully (Exception handling code is not included.. )-

Configuration info in server-config.wsdd

<service name="DataService" style="message">
<parameter name="className" value="webservice.RequestManager"/>
<parameter name="allowedMethods" value="invokeService"/>
</service>

Notice that style is set to message. So you can exchange generic XML message with your client.

The invokeService method of webservice class will look something like this

public Document invokeService(Document doc) throws SOAPException {

Document response = null;

try {
Element element = doc.getDocumentElement();

// dataSerivce is the class that talks to database.
// Can be normal Java Class / Session bean anything that
// creates the XML org.w3c.dom.Document Object

DataService dataService = new DataService();

/**
*In my case the actual message that client sends has
*dataservice as the root tag
*/
NodeList xmlPayload = element
.getElementsByTagNameNS(properties.getDefaultNameSpace() ,
"dataservice");

DocumentBuilder builder = getDocumentBuilder();

/*
Create a Document Object From It.
*/

Document xmlRequest = builder.parse( new InputSource(new
StringReader(DOM2Writer.nodeToString(xmlPayload.item
(0),false))));

/*
Get Response.
*/

response = dataService.invokeService(xmlRequest);

System.out.println(
XMLUtils.DocumentToString(response));
}
catch (Exception ex) {
ex.printStackTrace();
throw new SOAPException (ex.getMessage());
}

return response ;

}// end invokeService


private DocumentBuilder getDocumentBuilder() throws DataServiceException {
DocumentBuilder builder = null;
try {
DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
df.setValidating(false);
df.setNamespaceAware(true);
builder = df.newDocumentBuilder();
} catch (.... ){}
return builder
}

And your client program will something like this -

public static void main(String[] args) {
try {

DocumentBuilderFactory df = DocumentBuilderFactory.newInstanc();
df.setValidating(false);
df.setNamespaceAware(true);
DocumentBuilder builder = df.newDocumentBuilder();
// This is the sample XML request
// read from file. IN real life client will have its own logic
// to generate it

Document doc = builder.parse(new File("ds.xml"));
Service service = new Service();
Call call = (Call) service.createCall();
call.setTimeout(new Integer(3080000));
String endpoint
= "http://<serverName> ort/MyWebApp/services/DataService";

call.setTargetEndpointAddress(new URL(endpoint));

Vector result = (Vector) call.invoke
(new Object[] {new SOAPBodyElement(doc.getDocumentElement())});

SOAPBodyElement sbe = (SOAPBodyElement) result.get(0);

Document resultDoc = sbe.getAsDocument();

//System.out.println("received " + XMLUtils.DocumentToString(resultDoc));

} catch ( Exception e ) {
e.printStackTrace();
System.out.println("Error " + e.getMessage());

}



Hope this helps. You can exchange any generic XML message with this.


nilesh<br />neilindallas@hotmail.com
Dharamvir (DV) Punia
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Hi Priya,
I am sorry that you got confused, Yes you are right, you have to set the return type before invoking the service.

e.g
// set the return type first, in this case you are setting your type to String


// now invoke the service and get the return value and assign to whatever your return type is , in this case its a string as you set your type to String.


Is that good. No Confusion now

-DV
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Thanks Neelesh for giving a good example. I will implement teh web service and get back for problems.

Thanks Dharamvir for getting back. Regards,
siva kumar
Ranch Hand

Joined: Sep 03, 2004
Posts: 86
Where Can i found the good examples for webservices using Axis
siva kumar
Ranch Hand

Joined: Sep 03, 2004
Posts: 86
Hi Priya

Can you give some webservices server examples
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hi Shiv Kumar -

I have seen the jws files in teh web-app directory of axis and have been working with that.

The steps for building a web service are given in the docs folder of axis.
They are all html files. Particularly the user-guide.html

That is where I have started.

Thanks,
Priya
siva kumar
Ranch Hand

Joined: Sep 03, 2004
Posts: 86
Originally posted by Hari priya:
Hi Shiv Kumar -

I have seen the jws files in teh web-app directory of axis and have been working with that.

The steps for building a web service are given in the docs folder of axis.
They are all html files. Particularly the user-guide.html

That is where I have started.

Thanks,
Priya
siva kumar
Ranch Hand

Joined: Sep 03, 2004
Posts: 86
hi priya

thnks
sakthi vadivelan
Greenhorn

Joined: Nov 15, 2004
Posts: 14
Dear seniors

Server side:
i have one imlimentation class and interface class in implimentation class i have to get the SOAP message object from client and process and give the result SOAP message to the client.In interface i declare that method"onMessage" it returns SOAPMessage object.

Client side:
i have one client class and it read the xml file and convert to the soap and i create the object for the server side interface class and using this object i pass the client side soap message object to the server iinterface.and process it

in this i got the soap message but i cant send the SOAPMessage object to this server interface......please any one give me the methd to send the soap messager object to the server interface class

"""ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service helloService = serviceFactory.createService(helloWsdlUrl,new QName(nameSpaceUri, serviceName));
pkgClient.JAXMServiceIF objSoap=(pkgClient.JAXMServiceIF);
helloService.getPort(new QName(nameSpaceUri, portName),pkgClient.JAXMServiceIF.class);
SOAPMessage soapResult=objSoap.onMessage(msg);""""

this my code to invoke the server interface JAXMServiceIF..i create one object for the interface class in client pkgClient is my client package


it gave me the error in the SOAPMessahge soapResult = objSoap.onmessage(msg)

the error is :: cannot be applied to SOAPMessage soapResult = objSoap.onMessage(msg)


please give me the solution


Sakthi.C
sakthi vadivelan
Greenhorn

Joined: Nov 15, 2004
Posts: 14
hello hari priya mam please give me the idea or any member please give me the idea about how to send a soapmessage to server without using connection.call method

thanks a lot
lallitha devi
Greenhorn

Joined: Nov 28, 2004
Posts: 8
Hello sir/madam,

Now i want to send a soap messsage from client to server. Our server has one interface class and one implementation class.

Connection.call() method is not used for our requirement. because this method dirctly specifie the implementaion calss. but we need to specifie the interface class.Thus is our requirement.
lallitha devi
Greenhorn

Joined: Nov 28, 2004
Posts: 8
hello all,

my requirement is send soap message from client to server. so here im using onMessage() method. but this method doesn't support.so now i am using call.invoke(); here im passing soap envelope. now also i am getting 2 error like this


1. [exec] error: Type "javax.xml.soap.SOAPElement" implements more than one interface, interfaces: "javax.xml.soap.Nod
e", "org.w3c.dom.Element" ...
[exec] Result: 1

2. [exec] error: XML reader error: java.io.EOFException: Unexpected end of ZLIB input stream

Give me ur valuable idea.......
thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sending requests in xml