Produce and Consume JAX-WS SOAP Web Service example
JAX-WS is a Standard Java API for building web services and clients that communicate using XML. These communications could be message-oriented as well as Remote Procedure Call-oriented. These messages are transmitted as SOAP messages (XML files) over HTTP.
Project Structure
src
|--main
| +--java
| +--com
| +--memorynotfound
| +--client
| |--GreetingClient.java
| +--ws
| |--OrderService.java
| |--OrderServiceImpl.java
| |--WebServicePublisher.java
| +--resources
pom.xml
JAX-WS SOAP Web Service
Creating the endpoint interface
package com.memorynotfound.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import java.util.List;
@WebService
@SOAPBinding(style = Style.RPC)
public interface OrderService {
@WebMethod
String[] getOrders();
@WebMethod
boolean addOrder(String order);
}
Creating the endpoint implementation
package com.memorynotfound.ws;
import javax.jws.WebService;
@WebService(endpointInterface = "com.memorynotfound.ws.OrderService")
public class OrderServiceImpl implements OrderService {
@Override
public String[] getOrders() {
return new String[]{"SSD", "Graphic Card", "GPU"};
}
@Override
public boolean addOrder(String order) {
System.out.println("Saving new order: " + order);
return true;
}
}
Publishing the web service
package com.memorynotfound.ws;
import javax.xml.ws.Endpoint;
public class WebServicePublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8888/service/order", new OrderServiceImpl());
}
}
Testing the Web Service
When you run the WebServicePublisher the web services will be deployed and you can test it by accessing the generated WSDL. URL: http://localhost:8888/service/order?wsdl.
I posted the WSDL here for verification.
<?xml version="1.0" encoding="utf-16"?>
<!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
<!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
<definitions targetNamespace="http://ws.memorynotfound.com/" name="OrderServiceImplService" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.memorynotfound.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>
<xsd:schema>
</xsd:import namespace="http://jaxb.dev.java.net/array" schemaLocation="http://localhost:8888/service/order?xsd=1">
</xsd:schema>
</types>
<message name="addOrder">
</part name="arg0" type="xsd:string">
</message>
<message name="addOrderResponse">
</part name="return" type="xsd:boolean">
</message>
</message name="getOrders">
<message name="getOrdersResponse">
</part name="return" type="ns1:stringArray" xmlns:ns1="http://jaxb.dev.java.net/array">
</message>
<portType name="OrderService">
<operation name="addOrder">
</input wsam:Action="http://ws.memorynotfound.com/OrderService/addOrderRequest" message="tns:addOrder">
</output wsam:Action="http://ws.memorynotfound.com/OrderService/addOrderResponse" message="tns:addOrderResponse">
</operation>
<operation name="getOrders">
</input wsam:Action="http://ws.memorynotfound.com/OrderService/getOrdersRequest" message="tns:getOrders">
</output wsam:Action="http://ws.memorynotfound.com/OrderService/getOrdersResponse" message="tns:getOrdersResponse">
</operation>
</portType>
<binding name="OrderServiceImplPortBinding" type="tns:OrderService">
</soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc">
<operation name="addOrder">
</soap:operation soapAction="">
<input>
</soap:body use="literal" namespace="http://ws.memorynotfound.com/">
</input>
<output>
</soap:body use="literal" namespace="http://ws.memorynotfound.com/">
</output>
</operation>
<operation name="getOrders">
</soap:operation soapAction="">
<input>
</soap:body use="literal" namespace="http://ws.memorynotfound.com/">
</input>
<output>
</soap:body use="literal" namespace="http://ws.memorynotfound.com/">
</output>
</operation>
</binding>
<service name="OrderServiceImplService">
<port name="OrderServiceImplPort" binding="tns:OrderServiceImplPortBinding">
</soap:address location="http://localhost:8888/service/order">
</port>
</service>
</definitions>
Web Service Clients
Now let’s see how we can consume a web service.
Consuming JAX-WS-SOAP Web Service
package com.memorynotfound.client;
import com.memorynotfound.ws.OrderService;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;
import java.util.Arrays;
public class GreetingClient {
public static void main(String[] args) throws Exception {
URL wsdlUrl = new URL("http://localhost:8888/service/order?wsdl");
QName qname = new QName("http://ws.memorynotfound.com/", "OrderServiceImplService");
Service service = Service.create(wsdlUrl, qname);
OrderService orderService = service.getPort(OrderService.class);
System.out.println(Arrays.toString(orderService.getOrders()));
System.out.println("Order completed: " + orderService.addOrder("Mother Board"));
}
}
Consuming JAX-WS-SOAP Web Service From WSDL
Open your terminal and enter the following:
- The wsimport is the command and needs to be on the class path in order to execute it.
- The -keep option will save the source files to disk.
- The -verbose option is optional this will print out more information, very handy when you don’t receive any files or get an error.
wsimport -keep -verbose http://localhost:8888/service/order?wsdl
This will generate the necessary files to consume the web service. Note that in my eyes this sometimes generates the wrong data structures and that it needs a little bit of tweaking to get it right. But for simple situations like this it is oké.
Generated JAX-WS Soap endpoint using wsimportpackage com.memorynotfound.client;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Action;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.4-b01
* Generated source version: 2.2
*
*/
@WebService(name = "OrderService", targetNamespace = "http://ws.memorynotfound.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface OrderService {
/**
*
* @param arg0
* @return
* returns boolean
*/
@WebMethod
@WebResult(partName = "return")
@Action(input = "http://ws.memorynotfound.com/OrderService/addOrderRequest", output = "http://ws.memorynotfound.com/OrderService/addOrderResponse")
public boolean addOrder(
@WebParam(name = "arg0", partName = "arg0")
String arg0);
/**
*
* @return
* returns net.java.dev.jaxb.array.StringArray
*/
@WebMethod
@WebResult(partName = "return")
@Action(input = "http://ws.memorynotfound.com/OrderService/getOrdersRequest", output = "http://ws.memorynotfound.com/OrderService/getOrdersResponse")
public String[] getOrders();
}
Generated JAX-WS Soap endpoint implementation using wsimportpackage com.memorynotfound.client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.4-b01
* Generated source version: 2.2
*
*/
@WebServiceClient(name = "OrderServiceImplService", targetNamespace = "http://ws.memorynotfound.com/", wsdlLocation = "http://localhost:8888/service/order?wsdl")
public class OrderServiceImplService extends Service {
private final static URL ORDERSERVICEIMPLSERVICE_WSDL_LOCATION;
private final static WebServiceException ORDERSERVICEIMPLSERVICE_EXCEPTION;
private final static QName ORDERSERVICEIMPLSERVICE_QNAME = new QName("http://ws.memorynotfound.com/", "OrderServiceImplService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://localhost:8888/service/order?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
ORDERSERVICEIMPLSERVICE_WSDL_LOCATION = url;
ORDERSERVICEIMPLSERVICE_EXCEPTION = e;
}
public OrderServiceImplService() {
super(__getWsdlLocation(), ORDERSERVICEIMPLSERVICE_QNAME);
}
public OrderServiceImplService(WebServiceFeature... features) {
super(__getWsdlLocation(), ORDERSERVICEIMPLSERVICE_QNAME, features);
}
public OrderServiceImplService(URL wsdlLocation) {
super(wsdlLocation, ORDERSERVICEIMPLSERVICE_QNAME);
}
public OrderServiceImplService(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, ORDERSERVICEIMPLSERVICE_QNAME, features);
}
public OrderServiceImplService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public OrderServiceImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return
* returns OrderService
*/
@WebEndpoint(name = "OrderServiceImplPort")
public OrderService getOrderServiceImplPort() {
return super.getPort(new QName("http://ws.memorynotfound.com/", "OrderServiceImplPort"), OrderService.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the features
parameter will have their default values.
* @return
* returns OrderService
*/
@WebEndpoint(name = "OrderServiceImplPort")
public OrderService getOrderServiceImplPort(WebServiceFeature... features) {
return super.getPort(new QName("http://ws.memorynotfound.com/", "OrderServiceImplPort"), OrderService.class, features);
}
private static URL __getWsdlLocation() {
if (ORDERSERVICEIMPLSERVICE_EXCEPTION!= null) {
throw ORDERSERVICEIMPLSERVICE_EXCEPTION;
}
return ORDERSERVICEIMPLSERVICE_WSDL_LOCATION;
}
}
Using the generated endpointspackage com.memorynotfound.client;
import java.util.Arrays;
public class GreetingClient {
public static void main(String... args){
OrderServiceImplService orderServiceImplService = new OrderServiceImplService();
OrderService orderService = orderServiceImplService.getOrderServiceImplPort();
System.out.println(Arrays.toString(orderService.getOrders()));
System.out.println("Order added: " + orderService.addOrder("Keyboard"));
}
}
References
Download Source Code
Download it – JAX-WS Consume and Produce Web Service
package com.memorynotfound.client;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Action;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.4-b01
* Generated source version: 2.2
*
*/
@WebService(name = "OrderService", targetNamespace = "http://ws.memorynotfound.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface OrderService {
/**
*
* @param arg0
* @return
* returns boolean
*/
@WebMethod
@WebResult(partName = "return")
@Action(input = "http://ws.memorynotfound.com/OrderService/addOrderRequest", output = "http://ws.memorynotfound.com/OrderService/addOrderResponse")
public boolean addOrder(
@WebParam(name = "arg0", partName = "arg0")
String arg0);
/**
*
* @return
* returns net.java.dev.jaxb.array.StringArray
*/
@WebMethod
@WebResult(partName = "return")
@Action(input = "http://ws.memorynotfound.com/OrderService/getOrdersRequest", output = "http://ws.memorynotfound.com/OrderService/getOrdersResponse")
public String[] getOrders();
}
package com.memorynotfound.client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.4-b01
* Generated source version: 2.2
*
*/
@WebServiceClient(name = "OrderServiceImplService", targetNamespace = "http://ws.memorynotfound.com/", wsdlLocation = "http://localhost:8888/service/order?wsdl")
public class OrderServiceImplService extends Service {
private final static URL ORDERSERVICEIMPLSERVICE_WSDL_LOCATION;
private final static WebServiceException ORDERSERVICEIMPLSERVICE_EXCEPTION;
private final static QName ORDERSERVICEIMPLSERVICE_QNAME = new QName("http://ws.memorynotfound.com/", "OrderServiceImplService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://localhost:8888/service/order?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
ORDERSERVICEIMPLSERVICE_WSDL_LOCATION = url;
ORDERSERVICEIMPLSERVICE_EXCEPTION = e;
}
public OrderServiceImplService() {
super(__getWsdlLocation(), ORDERSERVICEIMPLSERVICE_QNAME);
}
public OrderServiceImplService(WebServiceFeature... features) {
super(__getWsdlLocation(), ORDERSERVICEIMPLSERVICE_QNAME, features);
}
public OrderServiceImplService(URL wsdlLocation) {
super(wsdlLocation, ORDERSERVICEIMPLSERVICE_QNAME);
}
public OrderServiceImplService(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, ORDERSERVICEIMPLSERVICE_QNAME, features);
}
public OrderServiceImplService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public OrderServiceImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return
* returns OrderService
*/
@WebEndpoint(name = "OrderServiceImplPort")
public OrderService getOrderServiceImplPort() {
return super.getPort(new QName("http://ws.memorynotfound.com/", "OrderServiceImplPort"), OrderService.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the features
parameter will have their default values.
* @return
* returns OrderService
*/
@WebEndpoint(name = "OrderServiceImplPort")
public OrderService getOrderServiceImplPort(WebServiceFeature... features) {
return super.getPort(new QName("http://ws.memorynotfound.com/", "OrderServiceImplPort"), OrderService.class, features);
}
private static URL __getWsdlLocation() {
if (ORDERSERVICEIMPLSERVICE_EXCEPTION!= null) {
throw ORDERSERVICEIMPLSERVICE_EXCEPTION;
}
return ORDERSERVICEIMPLSERVICE_WSDL_LOCATION;
}
}
Using the generated endpointspackage com.memorynotfound.client;
import java.util.Arrays;
public class GreetingClient {
public static void main(String... args){
OrderServiceImplService orderServiceImplService = new OrderServiceImplService();
OrderService orderService = orderServiceImplService.getOrderServiceImplPort();
System.out.println(Arrays.toString(orderService.getOrders()));
System.out.println("Order added: " + orderService.addOrder("Keyboard"));
}
}
References
Download Source Code
Download it – JAX-WS Consume and Produce Web Service
package com.memorynotfound.client;
import java.util.Arrays;
public class GreetingClient {
public static void main(String... args){
OrderServiceImplService orderServiceImplService = new OrderServiceImplService();
OrderService orderService = orderServiceImplService.getOrderServiceImplPort();
System.out.println(Arrays.toString(orderService.getOrders()));
System.out.println("Order added: " + orderService.addOrder("Keyboard"));
}
}