web services
soap vs rest
wsdl
jax-ws vs jax-rs
----------------------------------------------------------
JAX-WS Hello World Example – RPC Style
JAX-WS
JAX-WS is bundled with JDK 1.6, which makes Java web service development easier to develop. This tutorial shows you how to do the following tasks:
- Create a SOAP-based RPC style web service endpoint by using JAX-WS.
- Create a Java web service client manually.
- Create a Java web service client via wsimport tool.
- Create a PHP web service client.
You will be surprise of how simple it is to develop a RPC style web service in JAX-WS.
Note
In general words, “web service endpoint” is a service which published outside for user to access; where “web service client” is the party who access the published service.
JAX-WS Web Service End Point
The following steps showing how to use JAX-WS to create a RPC style web service endpoint.
You can use IDE like eclipse, to create a dynamic project (maybe its not required, i did so)
1. Create a Web Service Endpoint Interface
File : HelloWorld.java
- package net.cublog.ws;
-
-
import javax.jws.WebMethod;
-
import javax.jws.WebService;
-
import javax.jws.soap.SOAPBinding;
-
import javax.jws.soap.SOAPBinding.Style;
-
-
//Service Endpoint Interface
-
@WebService
-
@SOAPBinding(style = Style.RPC)
-
public interface HelloWorld{
-
-
@WebMethod String getHelloWorldAsString(String name);
-
-
}
2. Create a Web Service Endpoint Implementation
File : HelloWorldImpl.java
- package net.cublog.ws;
-
-
import javax.jws.WebService;
-
-
//Service Implementation
-
@WebService(endpointInterface = "net.cublog.ws.HelloWorld")
-
public class HelloWorldImpl implements HelloWorld{
-
-
@Override
-
public String getHelloWorldAsString(String name) {
-
return "Hello World JAX-WS " + name;
-
}
-
-
}
3. Create a Endpoint Publisher
File : HelloWorldPublisher.java
- package net.cublog.endpoint;
-
-
import javax.xml.ws.Endpoint;
-
-
import net.cublog.ws.HelloWorldImpl;
-
-
//Endpoint publisher
-
public class HelloWorldPublisher{
-
-
public static void main(String[] args) {
-
Endpoint.publish("", new HelloWorldImpl());
-
}
-
-
}
Run the endpoint publisher, and your “hello world web service” is deployed in URL ““.
(when i see the publish url, i confused about where the related file placed?)
4. Test It
You can test the deployed web service by accessing the generated WSDL (Web Service Definition Language) document via this URL “” .
Web Service Clients
Ok, web service is deployed properly, now let’s see how to create web service client to access to the published service.
1. Java Web Service Client
Without tool, you can create a Java web service client like this :
- package net.cublog.client;
-
import java.net.URL;
-
import javax.xml.namespace.QName;
-
import javax.xml.ws.Service;
-
-
import net.cublog.ws.HelloWorld;
-
-
-
public class HelloWorldClient {
-
-
public static void main(String[] args) throws Exception {
-
-
URL url = new URL("?wsdl");
-
-
//1st argument service URI, refer to wsdl document above
-
//2nd argument is service name, refer to wsdl document above
-
QName qname = new QName("http://ws.cublog.net/", "HelloWorldImplService");
-
-
Service service = Service.create(url, qname);
-
HelloWorld hello = service.getPort(HelloWorld.class);
-
System.out.println(hello.getHelloWorldAsString("henry"));
-
-
}
-
-
}
Output
Hello World JAX-WS mkyong
2. Java Web Service Client via wsimport tool
Alternative, you can use “wsimport” tool to parse the published wsdl file, and generate necessary client files (stub) to access the published web service.
Where is wsimport?
This wsimport tool is bundle with the JDK, you can find it at “JDK_PATH/bin” folder.
Issue “wsimport” command.
wsimport -keep http://localhost:9999/ws/hello?wsdl
It will generate necessary client files, which is depends on the provided wsdl file. In this case, it will generate one interface and one service implementation file.
File : HelloWorld.java
- package net.cublog.ws;
-
-
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 = "HelloWorld", targetNamespace = "http://ws.cublog.net/")
-
@SOAPBinding(style = SOAPBinding.Style.RPC)
-
public interface HelloWorld {
-
-
-
/**
-
*
-
* @param arg0
-
* @return
-
* returns java.lang.String
-
*/
-
@WebMethod
-
@WebResult(partName = "return")
-
@Action(input = "http://ws.cublog.net/HelloWorld/getHelloWorldAsStringRequest", output = "http://ws.cublog.net/HelloWorld/getHelloWorldAsStringResponse")
-
public String getHelloWorldAsString(
-
@WebParam(name = "arg0", partName = "arg0")
-
String arg0);
-
-
}
File : HelloWorldImplService.java
- package net.cublog.ws;
-
-
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 = "HelloWorldImplService", targetNamespace = "http://ws.cublog.net/", wsdlLocation = "?wsdl")
-
public class HelloWorldImplService
-
extends Service
-
{
-
-
private final static URL HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
-
private final static WebServiceException HELLOWORLDIMPLSERVICE_EXCEPTION;
-
private final static QName HELLOWORLDIMPLSERVICE_QNAME = new QName("http://ws.cublog.net/", "HelloWorldImplService");
-
-
static {
-
URL url = null;
-
WebServiceException e = null;
-
try {
-
url = new URL("?wsdl");
-
} catch (MalformedURLException ex) {
-
e = new WebServiceException(ex);
-
}
-
HELLOWORLDIMPLSERVICE_WSDL_LOCATION = url;
-
HELLOWORLDIMPLSERVICE_EXCEPTION = e;
-
}
-
-
public HelloWorldImplService() {
-
super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME);
-
}
-
-
public HelloWorldImplService(WebServiceFeature... features) {
-
super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME, features);
-
}
-
-
public HelloWorldImplService(URL wsdlLocation) {
-
super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME);
-
}
-
-
public HelloWorldImplService(URL wsdlLocation, WebServiceFeature... features) {
-
super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME, features);
-
}
-
-
public HelloWorldImplService(URL wsdlLocation, QName serviceName) {
-
super(wsdlLocation, serviceName);
-
}
-
-
public HelloWorldImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
-
super(wsdlLocation, serviceName, features);
-
}
-
-
/**
-
*
-
* @return
-
* returns HelloWorld
-
*/
-
@WebEndpoint(name = "HelloWorldImplPort")
-
public HelloWorld getHelloWorldImplPort() {
-
return super.getPort(new QName("http://ws.cublog.net/", "HelloWorldImplPort"), HelloWorld.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 HelloWorld
-
*/
-
@WebEndpoint(name = "HelloWorldImplPort")
-
public HelloWorld getHelloWorldImplPort(WebServiceFeature... features) {
-
return super.getPort(new QName("http://ws.cublog.net/", "HelloWorldImplPort"), HelloWorld.class, features);
-
}
-
-
private static URL __getWsdlLocation() {
-
if ( null) {
-
throw HELLOWORLDIMPLSERVICE_EXCEPTION;
-
}
-
return HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
-
}
-
-
}
Now, create a Java web service client which depends on the above generated files.
(You should copy the generated HelloWorldImplService.java[it is in the folder you exe the wsimport command ] to your package then create below client)
- package net.cublog.client;
-
-
import net.cublog.ws.HelloWorld;
-
import net.cublog.ws.HelloWorldImplService;
-
-
public class HelloWorldAnotherClient {
-
-
public static void main(String[] args) {
-
-
HelloWorldImplService helloService = new HelloWorldImplService();
-
HelloWorld hello = helloService.getHelloWorldImplPort();
-
-
System.out.println(hello.getHelloWorldAsString("henry"));
-
-
}
-
}
Here’s the output
阅读(2995) | 评论(0) | 转发(0) |