加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用axis在项目中开发WebService服务

发布时间:2020-12-17 02:45:04 所属栏目:安全 来源:网络整理
导读:?最近有个需求,要在现有的WEB服务器上提供一个WebService服务,找资料后确定有两种方法可行,一种是用axis,另一种是利用xfire,由于之前有同事用axis做过,并且比较简单,所以确定用axis进行开发.下面我就把用axis开发的过程简单的记录下来: 第一步:下载相关的jar

?最近有个需求,要在现有的WEB服务器上提供一个WebService服务,找资料后确定有两种方法可行,一种是用axis,另一种是利用xfire,由于之前有同事用axis做过,并且比较简单,所以确定用axis进行开发.下面我就把用axis开发的过程简单的记录下来:

第一步:下载相关的jar包,axis要用到的jar包有:axis.jar,activation.jar,commons-discovery.jar,jaxrpc.jar,wsdl4j.jar,saaj.jar(这个包是在运行客户端是用到的),这些jar包在axis的安装包里的lib下应该有的,接下来就是要把这些jar包部署到web应用的classpath中,例如部署到/web-inf/lib下.

第二步:修改web应用的web.xml配置文件,加上一个sevlet

<!-- ?利用axis做WEBSERVICE服务?20080219加入 -->

??
< servlet >

????
< servlet - name > AxisServlet </ servlet - name >

????
< display - name > Apache - Axis?Servlet </ display - name >

????
< servlet - class >

????????org.apache.axis.transport.http.AxisServlet

????
</ servlet - class >

??
</ servlet > ????
?
?<!-- 利用axis做WEBSERVICE服务 20080219加入-->
?????? <!-- 下面的*.jws映射配置在本次应用中没有用到,可以不加-->
?<servlet-mapping>
?? <servlet-name>AxisServlet</servlet-name>
??? <url-pattern>*.jws</url-pattern>
? </servlet-mapping>
<!-- 我们将用下面的映射地址调用service服务,例如你配置的service服务的名称是TestService那么调用这个service的路径为http://localhost/,webServices/TestService,如果你的调用路径不同修改影射地址即可-->
? <servlet-mapping>
??? <servlet-name>AxisServlet</servlet-name>
??? <url-pattern>/webServices/*</url-pattern>
? </servlet-mapping>
?
<!-- 在文件的最后加上下面这段-->
?<!-- 利用axis做WEBSERVICE服务 20080219加入-->
? <mime-mapping>
??? <extension>wsdl</extension>
???? <mime-type>text/xml</mime-type>
? </mime-mapping>
? <mime-mapping>
??? <extension>xsd</extension>
??? <mime-type>text/xml</mime-type>
? </mime-mapping>
?

到此web.xml配置文件修改完成.

第三步:开发提供服务的java程序

?

package?com.test.webapp.webservice.server;




/**

?*?提供给PS商户校验用户密码的服务

?*?

?*?@author?

?*?@version?1.0,?

?*?@since?JDK?1.5.1

?
*/


public ? class ?PSCheckPsdService? {


????
public?PSCheckPsdService?()?{

????}


??
/**

?????*?

?????*?验证结果一数组的形式返回给用户

?????
*/

????
public?String[]?checkPsd(String?yourName,String?password)?{

????????String?result[]?
=?new?String[2];

????????
if("xiaoming".equals(telNum)&&"000000".equals(password)){

????????????result[
0]?=?"0";

????????????result[
1]?=?"ok";

????????}
else{

????????????result[
0]?=?"1";

????????????result[
1]?=?"密码不正确";

????????}

????????
return?result;

????}


}

?

?提供服务的java程序完成,是不是很简单呢,就是和普通的java程序一样,不用导入什么特殊的类文件.

第四步:开发webservice的配置文件server-config.wsdd,我也不知道是不是必须是这个文件名,没有用过别的文件名去试,文件内容如下:

<? xml?version = " 1.0 " ?encoding = " UTF-8 " ?>

< deployment?xmlns = " http://xml.apache.org/axis/wsdd/ " ?xmlns:java = " http://xml.apache.org/axis/wsdd/providers/java " >

?
< globalConfiguration >

??
< parameter?name = " adminPassword " ?value = " admin " />

??
< parameter?name = " attachments.implementation " ?value = " org.apache.axis.attachments.AttachmentsImpl " />

??
< parameter?name = " sendXsiTypes " ?value = " true " />

??
< parameter?name = " sendMultiRefs " ?value = " true " />

??
< parameter?name = " sendXMLDeclaration " ?value = " true " />

??
< parameter?name = " axis.sendMinimizedElements " ?value = " true " />

??
< requestFlow >

???
< handler?type = " java:org.apache.axis.handlers.JWSHandler " >

????
< parameter?name = " scope " ?value = " session " />

???
</ handler >

???
< handler?type = " java:org.apache.axis.handlers.JWSHandler " >

????
< parameter?name = " scope " ?value = " request " />

????
< parameter?name = " extension " ?value = " .jwr " />

???
</ handler >

??
</ requestFlow >

?
</ globalConfiguration >

?
< handler?name = " LocalResponder " ?type = " java:org.apache.axis.transport.local.LocalResponder " />

?
< handler?name = " URLMapper " ?type = " java:org.apache.axis.handlers.http.URLMapper " />

?
< handler?name = " Authenticate " ?type = " java:org.apache.axis.handlers.SimpleAuthenticationHandler " />

?
< service?name = " AdminService " ?provider = " java:MSG " >

??
< parameter?name = " allowedMethods " ?value = " AdminService " />

??
< parameter?name = " enableRemoteAdmin " ?value = " false " />

??
< parameter?name = " className " ?value = " org.apache.axis.utils.Admin " />

??
< namespace > http: // xml.apache.org/axis/wsdd/</namespace>

? </ service >

?
< service?name = " Version " ?provider = " java:RPC " >

??
< parameter?name = " allowedMethods " ?value = " getVersion " />

??
< parameter?name = " className " ?value = " org.apache.axis.Version " />

?
</ service >

?
<!-- ?定义webservice服务?20080219加入,这就是配置我们开发的service的地方,我们开发了新的service服务只要增加一个service节点即可 -->

?
< service?name = " TestService " ?provider = " java:RPC " ><!--定义service的名称如:TestService-->

??
< parameter?name = " allowedMethods " ?value = " * " /><!--允许提供服务的方法名称,有多个用逗号阁开,*表示所有方法-->

??
< parameter?name = " scope " ?value = " Request " /><!--Request,Application,Session-->

??
< parameter?name = " className " ?value = " com.test.webapp.webservice.server.PSCheckPsdService " />

?
</ service >

?

?
< transport?name = " http " >

??
< requestFlow >

???
< handler?type = " URLMapper " />

???
< handler?type = " java:org.apache.axis.handlers.http.HTTPAuthHandler " />

??
</ requestFlow >

?
</ transport >

?
< transport?name = " local " >

??
< responseFlow >

???
< handler?type = " LocalResponder " />

??
</ responseFlow >

?
</ transport >

</ deployment >

配置文件server-config.wsdd开发完成.

第五步:部署类文件到/web-inf/classes/中,重启服务

第六步测试:在浏览器中输入地址:?wsdl 测试service是否部署成功,如果出现如下结果即表示成功:

?? <? xml?version = " 1.0 " ?encoding = " UTF-8 " ? ?> ?

- ? < wsdl:definitions?targetNamespace = " http://localhost/webServices/TestService " ?xmlns = " http://schemas.xmlsoap.org/wsdl/ " ?xmlns:apachesoap = " http://xml.apache.org/xml-soap " ?xmlns:impl = " http://localhost/webServices/TestService " ?xmlns:intf = " http://localhost/webServices/TestService " ?xmlns:soapenc = " http://schemas.xmlsoap.org/soap/encoding/ " ?xmlns:wsdl = " http://schemas.xmlsoap.org/wsdl/ " ?xmlns:wsdlsoap = " http://schemas.xmlsoap.org/wsdl/soap/ " ?xmlns:xsd = " http://www.w3.org/2001/XMLSchema " >

- ? < wsdl:types >

- ? < schema?targetNamespace = " http://localhost/webServices/TestService " ?xmlns = " http://www.w3.org/2001/XMLSchema " >

??
< import? namespace = " http://schemas.xmlsoap.org/soap/encoding/ " ? /> ?

- ? < complexType?name = " ArrayOf_xsd_string " >

- ? < complexContent >

- ? < restriction? base = " soapenc:Array " >

??
< attribute? ref = " soapenc:arrayType " ?wsdl:arrayType = " xsd:string[] " ? /> ?

??
</ restriction >

??
</ complexContent >

??
</ complexType >

??
</ schema >

??
</ wsdl:types >

- ? < wsdl:message?name = " checkTelPsdResponse " >

??
< wsdl:part?name = " checkTelPsdReturn " ?type = " intf:ArrayOf_xsd_string " ? /> ?

??
</ wsdl:message >

- ? < wsdl:message?name = " checkTelPsdRequest " >

??
< wsdl:part?name = " telNum " ?type = " xsd:string " ? /> ?

??
< wsdl:part?name = " password " ?type = " xsd:string " ? /> ?

??
</ wsdl:message >

- ? < wsdl:portType?name = " PSCheckTelPsdService " >

- ? < wsdl:operation?name = " checkTelPsd " ?parameterOrder = " telNum?password " >

??
< wsdl:input?message = " intf:checkTelPsdRequest " ?name = " checkTelPsdRequest " ? /> ?

??
< wsdl:output?message = " intf:checkTelPsdResponse " ?name = " checkTelPsdResponse " ? /> ?

??
</ wsdl:operation >

??
</ wsdl:portType >

- ? < wsdl:binding?name = " TestServiceSoapBinding " ?type = " intf:PSCheckPsdService " >

??
< wsdlsoap:binding?style = " rpc " ?transport = " http://schemas.xmlsoap.org/soap/http " ? /> ?

- ? < wsdl:operation?name = " checkTelPsd " >

??
< wsdlsoap:operation?soapAction = "" ? /> ?

- ? < wsdl:input?name = " checkTelPsdRequest " >

??
< wsdlsoap:body?encodingStyle = " http://schemas.xmlsoap.org/soap/encoding/ " ? namespace = " http://server.webservice.webapp.test.com " ?use = " encoded " ? /> ?

??
</ wsdl:input >

- ? < wsdl:output?name = " checkTelPsdResponse " >

??
< wsdlsoap:body?encodingStyle = " http://schemas.xmlsoap.org/soap/encoding/ " ? namespace = " http://localhost/webServices/TestService " ?use = " encoded " ? /> ?

??
</ wsdl:output >

??
</ wsdl:operation >

??
</ wsdl:binding >

- ? < wsdl:service?name = " PSCheckPsdServiceService " >

- ? < wsdl:port?binding = " intf:PSCheckPsdSVSoapBinding " ?name = " TestService " >

??
< wsdlsoap:address?location = " http://localhost/webServices/TestService " ? /> ?

??
</ wsdl:port >

??
</ wsdl:service >

??
</ wsdl:definitions >

第六步:用axis开发的客户测试是否可以调通

package?com.sztelecom.webapp.webservice.test;


import?org.apache.axis.client.Service;

import?org.apache.axis.client.Call;

import?javax.xml.
namespace .QName;

public ? class ?Test? {

????
public?static?void?main(String[]?args)?{

????????
try?{

????????String?name
=?"xiaoming";

????????String?password?
=?"000000";

????????String?endpoint?
=?"http://localhost/webServices/TestService";

????????Service?service?
=?new?Service();

????????Call?call?
=?(Call)service.createCall();

????????call.setTargetEndpointAddress(endpoint);

????????call.setOperationName(
new?QName("urn:TestService",?"checkPsd"));

????????String[]?result?
=?(String[])call.invoke(new?Object[]{name,password});

????????System.
out.println("AXIS?Server?return:?"?+?result[0]);

????????}
?catch(Exception?e)?{

????????e.printStackTrace();

????????}

????????????

????????}

}

运行该程序,看看打印出来的是不是AXIS?Server?return:??0

以上程序在weblogic6.1中测试通过,tomcat正在部署中,有结果将会帖出来的,欢迎讨论相关技术,QQ:271537491

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读