Returning multiple values from a web service

By: Emiley J Emailed: 1599 times Printed: 2051 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Although this is quite a common requirement, it could be quite a daunting task for a beginner learning web service. I have seen so many questions in various forums regarding this. For example, one popular question is how to return a HashMap from a web service. In this tutorial, I will keep it very simple. I will just return two values from the webservice instead of one.

The trick is to use javax.xml.ws.Holder and define @WebParam as an output parameter in your definition. An example is below.


import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.xml.ws.Holder;


/**
 *
 * @author Emiley
 */

@WebService(serviceName = "TestMultiple")
public class TestMultiple {
    @WebMethod(operationName = "Calculate")
    public void Calculate(@WebParam(name = "sum", mode = WebParam.Mode.OUT) Holder sum,
                               @WebParam(name = "multiply", mode = WebParam.Mode.OUT) Holder multiply) {
       sum.value = 4+5;
       multiply.value = 4*5;
    }
	}

When you call this web service, you will get the following output.

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:operationResponse xmlns:ns2="http://java-samples.com/">
         <sum>9</sum>
         <multiply>20</multiply>
      </ns2:operationResponse>
   </S:Body>
</S:Envelope>

Of course, you can make your web service more useful by adding two parameters to get the values and add them as below


import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.xml.ws.Holder;


/**
 *
 * @author Emiley
 */

@WebService(serviceName = "TestMultiple")
public class TestMultiple {
    @WebMethod(operationName = "Calculate")
    public void Calculate(int x, int y, @WebParam(name = "sum", mode = WebParam.Mode.OUT) Holder sum,
                               @WebParam(name = "multiply", mode = WebParam.Mode.OUT) Holder multiply) {
       sum.value = x+y;
       multiply.value = x*y;
    }
	}


WebServices Home | All WebServices Tutorials | Latest WebServices Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

I'm coding a webservice and this is exactly what I'm looking for.
I have coded an example as yours, but Im getting and error talling me that "sum" cannot be converted to "Holder" type.
Do you declare "sum" anywhere in your code?

Best regards


View Tutorial          By: Cristian at 2014-11-14 12:52:34

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Emiley J
Password must include both numeric and alphabetic characters - Magento
What is Hadoop?
Returning multiple values from a web service
Tomcat and httpd configured in port 8080 and 80
Java Webservices using Netbeans and Tomcat
Java WebService connected to Database
How to Deploy a Java Web Service
Call a webservice in Java
Java WebService - Create your first web service in Java
package javax.jws does not exist
Getting Started with Android
HTML5 Location - getCurrentPosition() in HTML5
HTML5 Canvas - Using Canvas in HTML5
HTML5 - Introduction
HTML5 Video - Handling video in HTML5

More Tutorials in WebServices
Returning multiple values from a web service
Java Webservices using Netbeans and Tomcat
How to Deploy a Java Web Service
Java WebService connected to Database
package javax.jws does not exist
Java WebService - Create your first web service in Java
Call a webservice in Java

More Latest News
Most Viewed Articles (in WebServices )
Java WebService connected to Database
Call a webservice in Java
Java Webservices using Netbeans and Tomcat
Returning multiple values from a web service
package javax.jws does not exist
How to Deploy a Java Web Service
Java WebService - Create your first web service in Java
Most Emailed Articles (in WebServices)
Call a webservice in Java
package javax.jws does not exist
Java WebService - Create your first web service in Java
How to Deploy a Java Web Service
Java WebService connected to Database
Java Webservices using Netbeans and Tomcat
Returning multiple values from a web service