学习Java6写WebServices

本文详细介绍如何使用Java6内置支持实现WebServices,包括服务端发布、客户端调用及在Tomcat中的部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://www.blogjava.net/vip01/archive/2006/12/21/89201.html

  Java6发布了,其中一个吸引我的新特性就是原生支持WebServices。在这和大家分享下学习心得。
下面就开始写个最简单的WebServices:
package  org.hermit.study.jdk;

import  javax.jws.WebMethod;
import  javax.jws.WebService;
import  javax.jws.soap.SOAPBinding;

@WebService(targetNamespace 
=   " http://jdk.study.hermit.org/client " )
@SOAPBinding(style 
=  SOAPBinding.Style.RPC)
public   class  Hello {
    @WebMethod
    
public  String sayHello(String name) {
        
return   " hello: "   +  name;
    }
}
怎么样简洁吧,很多朋友的写法还要在命令行中执行“ wsgen –cp . <path>
用偶这种方法写的service可以省去上面这步。

targetNamespace = "http://jdk.study.hermit.org/client"这句是指定客户端获取服务端服务后存放的类路径。注意是反着的,http: //jdk.study.hermit.org/client在客户端生成的类会放在org.hermit.study.jdk.client包下。
下面是发布服务:
package  org.hermit.study.jdk;

import  javax.xml.ws.Endpoint;

public   class  StartService  {
    
public static void main(String[] args) {
        Endpoint.publish(
"http://localhost:8080/HelloService"new Hello());
    }

}
呵呵,更简洁。一句话而已。
http://localhost:8080/HelloService是指发布的地址

运行
StartService  ,开发浏览器输入:http://localhost:8080/HelloService?wsdl



如果能看到以下内容,就可以
  <? xml version="1.0" encoding="UTF-8"  ?>  
< definitions  xmlns ="http://schemas.xmlsoap.org/wsdl/"  xmlns:tns ="http://jdk.study.hermit.org/client"  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:soap ="http://schemas.xmlsoap.org/wsdl/soap/"  targetNamespace ="http://jdk.study.hermit.org/client"  name ="HelloService" >
  
< types  />  
< message  name ="sayHello" >
  
< part  name ="arg0"  type ="xsd:string"   />  
  
</ message >
< message  name ="sayHelloResponse" >
  
< part  name ="return"  type ="xsd:string"   />  
  
</ message >
< portType  name ="Hello" >
< operation  name ="sayHello"  parameterOrder ="arg0" >
  
< input  message ="tns:sayHello"   />  
  
< output  message ="tns:sayHelloResponse"   />  
  
</ operation >
  
</ portType >
< binding  name ="HelloPortBinding"  type ="tns:Hello" >
  
< soap:binding  style ="rpc"  transport ="http://schemas.xmlsoap.org/soap/http"   />  
< operation  name ="sayHello" >
  
< soap:operation  soapAction =""   />  
< input >
  
< soap:body  use ="literal"  namespace ="http://jdk.study.hermit.org/client"   />  
  
</ input >
< output >
  
< soap:body  use ="literal"  namespace ="http://jdk.study.hermit.org/client"   />  
  
</ output >
  
</ operation >
  
</ binding >
< service  name ="HelloService" >
< port  name ="HelloPort"  binding ="tns:HelloPortBinding" >
  
< soap:address  location ="http://localhost:8080/HelloService"   />  
  
</ port >
  
</ service >
  
</ definitions >

Java6 WebServices (一)服务端 这篇文章中和大家分享了Java6 WebServices 服务端的写法,光有服务端还不行,还要有客户端才行啊。看了一些网友的相关文章,都只给出了服务端的写法,没有说客户端怎么说。经过一番研究,终于搞定了客户端,今天偶就和大家分享下。
首先启动 Java6 WebServices (一)服务端 一文中写好的服务端。
新建个项目。命令行到src目录执行,
wsimport -keep  http://localhost:8080/HelloService?wsdl
会自动生成一些代码。


package  org.hermit.study.jdk.client;

import  javax.jws.WebMethod;
import  javax.jws.WebParam;
import  javax.jws.WebResult;
import  javax.jws.WebService;
import  javax.jws.soap.SOAPBinding;


/**
 * This class was generated by the JAXWS SI.
 * JAX-WS RI 2.0_02-b08-fcs
 * Generated source version: 2.0
 * 
 
*/
@WebService(name 
=   " Hello " , targetNamespace  =   " http://jdk.study.hermit.org/client " )
@SOAPBinding(style 
=  SOAPBinding.Style.RPC)
public   interface  Hello {


    
/**
     * 
     * 
@param  arg0
     * 
@return
     *     returns java.lang.String
     
*/
    @WebMethod
    @WebResult(partName 
=   " return " )
    
public  String sayHello(
        @WebParam(name 
=   " arg0 " , partName  =   " arg0 " )
        String arg0);

}

package  org.hermit.study.jdk.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;


/**
 * This class was generated by the JAXWS SI.
 * JAX-WS RI 2.0_02-b08-fcs
 * Generated source version: 2.0
 * 
 
*/
@WebServiceClient(name 
=   " HelloService " , targetNamespace  =   " http://jdk.study.hermit.org/client " , wsdlLocation  =   " http://localhost:8080/HelloService?wsdl " )
public   class  HelloService
    
extends  Service
{

    
private   final   static  URL HELLOSERVICE_WSDL_LOCATION;

    
static  {
        URL url 
=   null ;
        
try  {
            url 
=   new  URL( " http://localhost:8080/HelloService?wsdl " );
        } 
catch  (MalformedURLException e) {
            e.printStackTrace();
        }
        HELLOSERVICE_WSDL_LOCATION 
=  url;
    }

    
public  HelloService(URL wsdlLocation, QName serviceName) {
        
super (wsdlLocation, serviceName);
    }

    
public  HelloService() {
        
super (HELLOSERVICE_WSDL_LOCATION,  new  QName( " http://jdk.study.hermit.org/client " " HelloService " ));
    }

    
/**
     * 
     * 
@return
     *     returns Hello
     
*/
    @WebEndpoint(name 
=   " HelloPort " )
    
public  Hello getHelloPort() {
        
return  (Hello) super .getPort( new  QName( " http://jdk.study.hermit.org/client " " HelloPort " ), Hello. class );
    }

}

下面我们再写个测试,看看能不能用:
package  org.hermit.study.jdk.client.test;

import  org.hermit.study.jdk.client.Hello;
import  org.hermit.study.jdk.client.HelloService;


public   class  TestClient {
    
public   static   void  main(String[] args) {
        HelloService service 
=   new  HelloService();
        Hello _hello 
=  service.getHelloPort();
        System.out.println(_hello.sayHello(
" hermit " ));
    }
}
执行。
控制台输出:
hello:hermit

ok!搞定。
java6搞定web service就这么简单。。。。。。。。。。。。。。。。。。。。。。。。。。

xfire危险了。。。。。。。。。。。。。。。。。。。。。。。。


为了满足广大网友的要求,今天抽时间搞了下WebServices 在tomcat中的发布
相关文章:
tomcat启动时自动加载servlet
学习Java6(一) WebServices (1)服务端
学习Java6(一) WebServices (2)客户端

新建一个servlet,偶太,能少打一个字符都是好的,所以servlet写的非常简洁,也适合初学者看得懂。。。。。。。。。。
WebServiceStarter.java

 1  import  javax.servlet.ServletException;
 2  import  javax.servlet.http.HttpServlet;
 3  import  javax.xml.ws.Endpoint;
 4 
 5  public   class  WebServiceStarter  extends  HttpServlet {
 6      
 7       private   static   final   long  serialVersionUID  =   5870534239093709659L ;
 8 
 9       public  WebServiceStarter() {
10           super ();
11      }
12 
13       public   void  destroy() {
14           super .destroy();
15      }
16 
17       public   void  init()  throws  ServletException {
18          System.out.println( " 准备启动服务 " );
19          Endpoint.publish( " http://localhost:8080/HelloService " new  Hello());
20          System.out.println( " 服务启动完毕 " );
21      }
22  }
23 

web service类Hello.java也是非常简单
 1 
 2 
 3  import  javax.jws.WebMethod;
 4  import  javax.jws.WebService;
 5  import  javax.jws.soap.SOAPBinding;
 6 
 7  @WebService(targetNamespace  =   " http://jdk.study.hermit.org/client " )
 8  @SOAPBinding(style  =  SOAPBinding.Style.RPC)
 9  public   class  Hello {
10      @WebMethod
11       public  String sayHello(String name) {
12           return   " hello: "   +  name;
13      }
14  }
web.xml
 1  <? xml version="1.0" encoding="UTF-8" ?>
 2  < web-app  version ="2.4"  xmlns ="http://java.sun.com/xml/ns/j2ee"
 3      xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
 4      xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee 
 5      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
 6       < servlet >
 7           < servlet-name > WebServiceStarter </ servlet-name >
 8           < servlet-class > WebServiceStarter </ servlet-class >
 9           < load-on-startup > 1 </ load-on-startup >
10       </ servlet >
11  </ web-app >
12 

ok
就这三个文件。。。。。。。。。啥jar都不要。。。。
发布,启动服务器
2007-1-5 13:28:37 org.apache.catalina.core.AprLifecycleListener init
信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: G:\JDK6\bin;F:\tomcat6\bin
2007-1-5 13:28:37 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2007-1-5 13:28:37 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 937 ms
2007-1-5 13:28:38 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2007-1-5 13:28:38 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.7
2007-1-5 13:28:38 org.apache.catalina.core.StandardHost start
信息: XML validation disabled
2007-1-5 13:28:38 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2007-1-5 13:28:38 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
准备启动服务
服务启动完毕
2007-1-5 13:28:39 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2007-1-5 13:28:39 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2007-1-5 13:28:39 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=16/62  config=null
2007-1-5 13:28:39 org.apache.catalina.startup.Catalina start
信息: Server startup in 1969 ms


访问: http://localhost:8080/HelloService?wsdl
 1     <? xml version="1.0" encoding="UTF-8"  ?>  
 2  < definitions  xmlns ="http://schemas.xmlsoap.org/wsdl/"  xmlns:tns ="http://jdk.study.hermit.org/client"  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:soap ="http://schemas.xmlsoap.org/wsdl/soap/"  targetNamespace ="http://jdk.study.hermit.org/client"  name ="HelloService" >
 3     < types  />  
 4  < message  name ="sayHello" >
 5     < part  name ="arg0"  type ="xsd:string"   />  
 6     </ message >
 7  < message  name ="sayHelloResponse" >
 8     < part  name ="return"  type ="xsd:string"   />  
 9     </ message >
10  < portType  name ="Hello" >
11  < operation  name ="sayHello"  parameterOrder ="arg0" >
12     < input  message ="tns:sayHello"   />  
13     < output  message ="tns:sayHelloResponse"   />  
14     </ operation >
15     </ portType >
16  < binding  name ="HelloPortBinding"  type ="tns:Hello" >
17     < soap:binding  style ="rpc"  transport ="http://schemas.xmlsoap.org/soap/http"   />  
18  < operation  name ="sayHello" >
19     < soap:operation  soapAction =""   />  
20  < input >
21     < soap:body  use ="literal"  namespace ="http://jdk.study.hermit.org/client"   />  
22     </ input >
23  < output >
24     < soap:body  use ="literal"  namespace ="http://jdk.study.hermit.org/client"   />  
25     </ output >
26     </ operation >
27     </ binding >
28  < service  name ="HelloService" >
29  < port  name ="HelloPort"  binding ="tns:HelloPortBinding" >
30     < soap:address  location ="http://localhost:8080/HelloService"   />  
31     </ port >
32     </ service >
33     </ definitions >
看到以上代码就ok!
客户端写法照旧

呵呵,这下大家满意了吧。。。。。。。。。
有冲动想把项目里的xfire撤掉了。。。。。。。。。。。。。。。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值