= @WebService OUT params via javax.xml.ws.Holder :jbake-date: 2016-09-06 :jbake-type: page :jbake-tomeepdf: :jbake-status: published Example webservice-holder can be browsed at https://github.com/apache/tomee/tree/master/examples/webservice-holder With SOAP it is possible to return multiple values in a single request. This is impossible in Java as a method can only return one object. JAX-WS solves this problem with the concept of Holders. A `javax.xml.ws.Holder` is a simple wrapper object that can be passed into the `@WebService` method as a parameter. The application sets the value of the holder during the request and the server will send the value back as an OUT parameter. == Using @WebParam and javax.xml.ws.Holder The `@WebParam` annotation allows us to declare the `sum` and `multiply` Holders as `WebParam.Mode.OUT` parameters. As mentioned, these holders are simply empty buckets the application can fill in with data to have sent to the client. The server will pass them in uninitialized. [source,java] ---- @Stateless @WebService( portName = "CalculatorPort", serviceName = "CalculatorService", targetNamespace = "http://superbiz.org/wsdl", endpointInterface = "org.superbiz.ws.out.CalculatorWs") public class Calculator implements CalculatorWs { public void sumAndMultiply(int a, int b, @WebParam(name = "sum", mode = WebParam.Mode.OUT) Holder sum, @WebParam(name = "multiply", mode = WebParam.Mode.OUT) Holder multiply) { sum.value = a + b; multiply.value = a * b; } } ---- If the Holders were specified as `WebParam.Mode.INOUT` params, then the client could use them to send data and the application as well. The `Holder` instances would then be initialized with the data from the client request. The application could check the data before eventually overriting it with the response values. == The WSDL The above JAX-WS `@WebService` component results in the folliwing WSDL that will be created automatically. Note the `sumAndMultiplyResponse` complext type returns two elements. These match the `@WebParam` declarations and our two `Holder` params. [source,xml] ---- ---- == Testing the OUT params Here we see a JAX-WS client executing the `sumAndMultiply` operation. Two empty `Holder` instances are created and passed in as parameters. The data from the `sumAndMultiplyResponse` is placed in the `Holder` instances and is then available to the client after the operation completes. The holders themselves are not actually sent in the request unless they are configured as INOUT params via WebParam.Mode.INOUT on `@WebParam` [source,java] ---- import org.junit.BeforeClass; import org.junit.Test; import javax.ejb.embeddable.EJBContainer; import javax.xml.namespace.QName; import javax.xml.ws.Holder; import javax.xml.ws.Service; import java.net.URL; import java.util.Properties; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; public class CalculatorTest { @BeforeClass public static void setUp() throws Exception { Properties properties = new Properties(); properties.setProperty("openejb.embedded.remotable", "true"); //properties.setProperty("httpejbd.print", "true"); //properties.setProperty("httpejbd.indent.xml", "true"); EJBContainer.createEJBContainer(properties); } @Test public void outParams() throws Exception { final Service calculatorService = Service.create( new URL("http://127.0.0.1:4204/Calculator?wsdl"), new QName("http://superbiz.org/wsdl", "CalculatorService")); assertNotNull(calculatorService); final CalculatorWs calculator = calculatorService.getPort(CalculatorWs.class); final Holder sum = new Holder(); final Holder multiply = new Holder(); calculator.sumAndMultiply(4, 6, sum, multiply); assertEquals(10, (int) sum.value); assertEquals(24, (int) multiply.value); } } ---- == Inspecting the messages The above execution results in the following SOAP message. === SOAP sumAndMultiply client request [source,xml] ---- 4 6 ---- === SOAP sumAndMultiplyResponse server response [source,xml] ---- 10 24 ----