Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2566487
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-11-22 11:40:27

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:

  1. Create a SOAP-based RPC style web service endpoint by using JAX-WS.
  2. Create a Java web service client manually.
  3. Create a Java web service client via wsimport tool.
  4. 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

  1. package net.cublog.ws;
  2.  
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebService;
  5. import javax.jws.soap.SOAPBinding;
  6. import javax.jws.soap.SOAPBinding.Style;
  7.  
  8. //Service Endpoint Interface
  9. @WebService
  10. @SOAPBinding(style = Style.RPC)
  11. public interface HelloWorld{
  12.  
  13.     @WebMethod String getHelloWorldAsString(String name);
  14.  
  15. }

2. Create a Web Service Endpoint Implementation

File : HelloWorldImpl.java

  1. package net.cublog.ws;
  2.  
  3. import javax.jws.WebService;
  4.  
  5. //Service Implementation
  6. @WebService(endpointInterface = "net.cublog.ws.HelloWorld")
  7. public class HelloWorldImpl implements HelloWorld{
  8.  
  9.     @Override
  10.     public String getHelloWorldAsString(String name) {
  11.         return "Hello World JAX-WS " + name;
  12.     }
  13.  
  14. }
3. Create a Endpoint Publisher

File : HelloWorldPublisher.java

  1. package net.cublog.endpoint;
  2.  
  3. import javax.xml.ws.Endpoint;

  4. import net.cublog.ws.HelloWorldImpl;
  5.  
  6. //Endpoint publisher
  7. public class HelloWorldPublisher{
  8.  
  9.     public static void main(String[] args) {
  10.      Endpoint.publish("", new HelloWorldImpl());
  11.     }
  12.  
  13. }

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 :

  1. package net.cublog.client;
  2. import java.net.URL;
  3. import javax.xml.namespace.QName;
  4. import javax.xml.ws.Service;

  5. import net.cublog.ws.HelloWorld;


  6. public class HelloWorldClient {

  7.     public static void main(String[] args) throws Exception {
  8.         
  9.         URL url = new URL("?wsdl");
  10.     
  11.      //1st argument service URI, refer to wsdl document above
  12.          //2nd argument is service name, refer to wsdl document above
  13.      QName qname = new QName("http://ws.cublog.net/", "HelloWorldImplService");
  14.     
  15.      Service service = Service.create(url, qname);
  16.      HelloWorld hello = service.getPort(HelloWorld.class);    
  17.      System.out.println(hello.getHelloWorldAsString("henry"));
  18.     
  19.      }

  20. }


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

  1. package net.cublog.ws;

  2. import javax.jws.WebMethod;
  3. import javax.jws.WebParam;
  4. import javax.jws.WebResult;
  5. import javax.jws.WebService;
  6. import javax.jws.soap.SOAPBinding;
  7. import javax.xml.ws.Action;


  8. /**
  9.  * This class was generated by the JAX-WS RI.
  10.  * JAX-WS RI 2.2.4-b01
  11.  * Generated source version: 2.2
  12.  *
  13.  */
  14. @WebService(name = "HelloWorld", targetNamespace = "http://ws.cublog.net/")
  15. @SOAPBinding(style = SOAPBinding.Style.RPC)
  16. public interface HelloWorld {


  17.     /**
  18.      *
  19.      * @param arg0
  20.      * @return
  21.      * returns java.lang.String
  22.      */
  23.     @WebMethod
  24.     @WebResult(partName = "return")
  25.     @Action(input = "http://ws.cublog.net/HelloWorld/getHelloWorldAsStringRequest", output = "http://ws.cublog.net/HelloWorld/getHelloWorldAsStringResponse")
  26.     public String getHelloWorldAsString(
  27.         @WebParam(name = "arg0", partName = "arg0")
  28.         String arg0);

  29. }

File : HelloWorldImplService.java

  1. package net.cublog.ws;

  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import javax.xml.namespace.QName;
  5. import javax.xml.ws.Service;
  6. import javax.xml.ws.WebEndpoint;
  7. import javax.xml.ws.WebServiceClient;
  8. import javax.xml.ws.WebServiceException;
  9. import javax.xml.ws.WebServiceFeature;


  10. /**
  11.  * This class was generated by the JAX-WS RI.
  12.  * JAX-WS RI 2.2.4-b01
  13.  * Generated source version: 2.2
  14.  *
  15.  */
  16. @WebServiceClient(name = "HelloWorldImplService", targetNamespace = "http://ws.cublog.net/", wsdlLocation = "?wsdl")
  17. public class HelloWorldImplService
  18.     extends Service
  19. {

  20.     private final static URL HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
  21.     private final static WebServiceException HELLOWORLDIMPLSERVICE_EXCEPTION;
  22.     private final static QName HELLOWORLDIMPLSERVICE_QNAME = new QName("http://ws.cublog.net/", "HelloWorldImplService");

  23.     static {
  24.         URL url = null;
  25.         WebServiceException e = null;
  26.         try {
  27.             url = new URL("?wsdl");
  28.         } catch (MalformedURLException ex) {
  29.             e = new WebServiceException(ex);
  30.         }
  31.         HELLOWORLDIMPLSERVICE_WSDL_LOCATION = url;
  32.         HELLOWORLDIMPLSERVICE_EXCEPTION = e;
  33.     }

  34.     public HelloWorldImplService() {
  35.         super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME);
  36.     }

  37.     public HelloWorldImplService(WebServiceFeature... features) {
  38.         super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME, features);
  39.     }

  40.     public HelloWorldImplService(URL wsdlLocation) {
  41.         super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME);
  42.     }

  43.     public HelloWorldImplService(URL wsdlLocation, WebServiceFeature... features) {
  44.         super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME, features);
  45.     }

  46.     public HelloWorldImplService(URL wsdlLocation, QName serviceName) {
  47.         super(wsdlLocation, serviceName);
  48.     }

  49.     public HelloWorldImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
  50.         super(wsdlLocation, serviceName, features);
  51.     }

  52.     /**
  53.      *
  54.      * @return
  55.      * returns HelloWorld
  56.      */
  57.     @WebEndpoint(name = "HelloWorldImplPort")
  58.     public HelloWorld getHelloWorldImplPort() {
  59.         return super.getPort(new QName("http://ws.cublog.net/", "HelloWorldImplPort"), HelloWorld.class);
  60.     }

  61.     /**
  62.      *
  63.      * @param features
  64.      * 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.
  65.      * @return
  66.      * returns HelloWorld
  67.      */
  68.     @WebEndpoint(name = "HelloWorldImplPort")
  69.     public HelloWorld getHelloWorldImplPort(WebServiceFeature... features) {
  70.         return super.getPort(new QName("http://ws.cublog.net/", "HelloWorldImplPort"), HelloWorld.class, features);
  71.     }

  72.     private static URL __getWsdlLocation() {
  73.         if ( null) {
  74.             throw HELLOWORLDIMPLSERVICE_EXCEPTION;
  75.         }
  76.         return HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
  77.     }

  78. }


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)

  1. package net.cublog.client;

  2. import net.cublog.ws.HelloWorld;
  3. import net.cublog.ws.HelloWorldImplService;

  4. public class HelloWorldAnotherClient {

  5.     public static void main(String[] args) {
  6.         
  7.         HelloWorldImplService helloService = new HelloWorldImplService();
  8.         HelloWorld hello = helloService.getHelloWorldImplPort();
  9.  
  10.         System.out.println(hello.getHelloWorldAsString("henry"));
  11.  
  12.     }
  13. }


Here’s the output

Hello World JAX-WS henry


阅读(2995) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~