栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

详解java开发webservice的几种方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解java开发webservice的几种方式

webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录。

1.Axis2

Axis是apache下一个开源的webservice开发组件,出现的算是比较早了,也比较成熟。这里主要介绍Axis+eclipse开发webservice,当然不用eclipse也可以开发和发布webservice,只是用eclipse会比较方便。

(1)下载eclipse的Java EE版本https://www.jb51.net/softs/239903.html#down

(2)下载axis2 http://axis.apache.org/axis2/java/core/download.cgi

(3)下载eclipse的axis2插件

Axis2_Codegen_Wizard
Axis2_Service_Archiver
http://axis.apache.org/axis2/java/core/tools/index.html
推荐使用1.3的版本

(4)eclipse安装axis2插件

1)在任意目录下新建一个Axis2文件夹,在该文件夹下新建eclipse目录,在eclipse目录中新建plugins目录和features目录,例如:D:programSoftwareeclipse-SVNAxis2eclipse;

2)把下载的axis2插件解压,并把解压的文件放到新建的eclipse的plugins目录下;

3)在%eclipse_home%的目录下新建links目录,并在links目录下新建axis2.link文件,内容为:path=D:programSoftwareeclipse-SVNAxis2;

4)重启eclipse,点击·file-new-other,如果看到Axis2 Wizards,则表明插件安装成功。

(5)安装axis2

下载Axis2的WAR Distribution并解压,把axis2.war包放置到%TOMCAT_HOME%/webapps下,启动tomcat,访问http://localhost:port/axis2,Axis2安装成功。

(6)使用eclipse新建web工程,创建一个普通java类,至少包含一个方法。

(7)发布webservice

1)点击eclipse的File-New-other,打开Axis2 Wizards,选择Axis2 Service Archiver,然后Next;

2)选择Class File Location,也就是类文件存放路径,注意:只选到classes目录,不要包括包文件夹,然后Next;

3)选择Skip WSDL,然后Next

4)一路Next到Select the Service XML file to be included in the Service archive,勾选Generate theservice xml automatically;

5)Service Name-填写你的service名称,Class Name-填写类名称,要包括包名,然后点击load,然后点击Finish,这时webservice就发布成功了;

6)然后到%TOMCAT_HOME%/webapps/axis2/WEB-INF/services 看看是否多了一个.aar的文件;

7)访问http://localhost:8085/axis2/services/类名?wsdl 就可看到生成的wsdl文件了。
注意:以上的方式是发布到axis2.war包中,你也可以把生成.aar文件copy到你的实际应用中,同时,你也可以使用eclipse的create webservice功能发布你的webservice,选择axis2生成你的webservice,这样webservice就会部署到你的应用中了。

2.Apche CXF

CXF开发webservice也是比较方便和简单的,它和spring的集成可以说是非常地好。举一个CXF开发webservice的例子吧。

1)在eclipse中新建一个web工程,导入依赖包。

2)编写一个接口,如:

public String test(@WebParam(name="value", targetNamespace = "http://service.cxf.zcl.com/", mode = WebParam.Mode.IN)String value);

注意:CXF开发的webservice,接口中的方法的参数一定要以这种方式,否则客户端调用的时候CXF服务端会接收不到参数的值,name:参数名称,可不写(建议写上),targetNamespace:命名空间,一定要填写上,默认是包名反过来的顺序,mode:参数类型,IN表示输入。

3)编写一个实现类,实现接口的方法;

4)和spring的集成,编写一个bean文件,如:cxf-beans.xml,内容如下:

  
  
 
    
    
    
    
    
 

 这个文件比较容易理解,就不解释了。

5)配置CXFServlet

在web.xml文件中配置CXFServlet,加载cxf-beans.xml文件,内容如下:

  
 
    
    contextConfigLocation  
    WEB-INF/cxf-beans.xml  
    
    
    
    org.springframework.web.context.ContextLoaderListener  
    
    
    
    cxf  
    org.apache.cxf.transport.servlet.CXFServlet  
    1  
    
    
    cxf  
    /services*.jar" />  
     
   
 
   
     
 
     
      
   
 
 
  
 
 
 
   
   
 
   
     
        
     
   
 
   
     
 
     
     
   
 
 

其引入的build.properties文件也在项目路径下

src.dir=${basedir}/src 
lib.dir=F:/WebService/xfire-1.2.6/lib 
wsdl.dir=http://localhost:8080/TestWebService/services/MessageService?wsdl 

其中lib.jar为我存放xfire的路径,运行ant得到代理对象

编写一个测试类

package com.lamp.test;  
 
import com.lamp.ws.client.MessageServiceClient;  
import com.lamp.ws.client.MessageServicePortType;  
 
public class TestGetName {  
 
  public static void main(String[] args) {  
    MessageServiceClient msg = new MessageServiceClient();  
    MessageServicePortType portType = msg.getMessageServiceHttpPort();  
    String result = portType.getName("张三");  
    System.out.println(result);  
  }  
 
} 
package com.lamp.test; 
 
import com.lamp.ws.client.MessageServiceClient; 
import com.lamp.ws.client.MessageServicePortType; 
 
public class TestGetName { 
 
  public static void main(String[] args) { 
    MessageServiceClient msg = new MessageServiceClient(); 
    MessageServicePortType portType = msg.getMessageServiceHttpPort(); 
    String result = portType.getName("张三"); 
    System.out.println(result); 
  } 
 
} 

运行在控制台看到了hellow 张三, welcome to WebService world至此一个简单的WebService开发完毕

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148596.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号