分享

AXIS框架,webservice应用

 WindySky 2011-07-11

AXIS框架,webservice应用--jws/WSDD

分类: 我的文章 730人阅读 评论(0) 收藏 举报

AXIS框架,webservice应用--jws/WSDD:
1.即时发布--JWS(java web service)
⑴编写服务端java类,然后将其改为后缀名为jws
⑵启动tomcat访问http://localhost/axisTest/xxx.jws可看到WSDL链接,说明发布成功
⑶编写客户端测试代码(使用Call类)


2.定制发布--WSDD
⑴编写服务端java类
package axisTest;

public class HelloWorldWSDD {
 public String hello(String name)
 {
  return "HELLO: "+name;
 }
}

⑵编写WEB-INF/deploy.wsdd文件(xml代码,包含<service>服务名、<parameter>类名/允许调用的方法)
<deployment xmlns="http://xml./axis/wsdd/"
 xmlns:java="http://xml./axis/wsdd/providers/java">
 <service name="HelloWorldWSDD" provider="java:RPC">
  <parameter name="className" value="axisTest.HelloWorldWSDD" />
  <parameter name="allowedMethods" value="*" />
  <parameter name="scope" value="request"></parameter>
 </service>
</deployment>

⑶cmd至WEB-INF下键入命令:java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient deploy.wsdd 会生成server-config.wsdd文件(应该使用命令:java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/axisTest/servlet/AxisServlet deploy.wsdd)

⑷可通过http://localhost:8080/axisTest/servlet/AxisServlet查看定制发布的服务
And now... Some Services

    * AdminService (wsdl)
          o AdminService
    * Version (wsdl)
          o getVersion
    * HelloWorldWSDD (wsdl)
          o hello

⑸编写客户端测试代码(使用Call类)
package axisTest;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

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

/**调用服务的客户端测试类
 * @author Tower
 *
 */
public class HelloWorldClient {

 /**
  * @param args
  * @throws ServiceException
  * @throws RemoteException
  */
 public static void main(String[] args) throws ServiceException, RemoteException {
  // TODO Auto-generated method stub
  String url="http://127.0.0.1:8080/axisTest/services/HelloWorldWSDD";
  Service service=new Service();
  Call call=(Call)service.createCall();
  call.setTargetEndpointAddress(url);
  
  call.setOperationName(new QName(url,"hello"));
  String result=(String)call.invoke(new Object[]{"tower888"});
  System.out.println(result);
 }

}
控制台输出结果:
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attach
ment support is disabled.
HELLO: tower888

-------------------------------------------------------
2(3)出现问题:键入上面2(3)的cmd命令不能自动生成server-config.wsdd文件,报404错:
E:/webWorkspace7.0/apache-tomcat-6.0.18/webapps/axisTest/WEB-INF>
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -p 80
80 deploy.wsdd
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disa
bled.
Processing file deploy.wsdd
Exception: AxisFault
 faultCode: {http://xml./axis/}HTTP
 faultSubcode:
 faultString: (404)Not Found
 faultActor:
 faultNode:
 faultDetail:
        {}:return code:  404
<html><head><title>Apache Tomcat/6.0.18 - Error report</title><style><!--H1 {font-family:Tahoma,A
rial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;backgrou
nd-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BO
DY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;ba
ckground-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.n
ame {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - /axis/services/
AdminService</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report<
/p><p><b>message</b> <u>/axis/services/AdminService</u></p><p><b>description&lt
;/b> <u>The requested resource (/axis/services/AdminService) is not available.</u></p><HR size="1&quo
t; noshade="noshade"><h3>Apache Tomcat/6.0.18</h3></body></html>
        {http://xml./axis/}HttpErrorCode:404

原因:没有在web.xml中添加servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java./xml/ns/j2ee"
 xmlns:xsi="http://www./2001/XMLSchema-instance"
 xsi:schemaLocation="http://java./xml/ns/j2ee
 http://java./xml/ns/j2ee/web-app_2_4.xsd">
 
 <servlet>
        <servlet-name>AxisServlet</servlet-name>
        <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

解决方法:
修改web.xml文件添加servlet后,并且在cmd命令中加入servlet地址,才能成功生成server-config.wsdd:
E:/webWorkspace7.0/apache-tomcat-6.0.18/webapps/axisTest/WEB-INF>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/axisTest/servlet/AxisServlet deploy.wsdd
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disa
bled.
Processing file deploy.wsdd
<Admin>Done processing</Admin>


--------tomcat中生成的server-config.wsdd文件,与deploy.wsdd、web.xml文件在相同目录---------
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml./axis/wsdd/" xmlns:java="http://xml./axis/wsdd/providers/java">
 <globalConfiguration>
  <parameter name="sendMultiRefs" value="true"/>
  <parameter name="disablePrettyXML" value="true"/>
  <parameter name="adminPassword" value="admin"/>
  <parameter name="attachments.Directory" value="E:/webWorkspace7.0/apache-tomcat-6.0.18/webapps/axisTest/WEB-INF/attachments"/>
  <parameter name="dotNetSoapEncFix" value="true"/>
  <parameter name="enableNamespacePrefixOptimization" value="false"/>
  <parameter name="sendXMLDeclaration" value="true"/>
  <parameter name="sendXsiTypes" value="true"/>
  <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
  <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./axis/wsdd/</namespace>
 </service>
 <service name="Version" provider="java:RPC">
  <parameter name="allowedMethods" value="getVersion"/>
  <parameter name="className" value="org.apache.axis.Version"/>
 </service>
 <service name="HelloWorldWSDD" provider="java:RPC">
  <parameter name="allowedMethods" value="*"/>
  <parameter name="scope" value="request"/>
  <parameter name="className" value="axisTest.HelloWorldWSDD"/>
 </service>
 <transport name="http">
  <requestFlow>
   <handler type="URLMapper"/>
   <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
  </requestFlow>
  <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/>
  <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
  <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>
  <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/>
  <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>
  <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
 </transport>
 <transport name="local">
  <responseFlow>
   <handler type="LocalResponder"/>
  </responseFlow>
 </transport>
</deployment>
----------------------------------------------------------
-----------------------------Handler应用------------------
deploy.wsdd:
<deployment xmlns="http://xml./axis/wsdd/"
 xmlns:java="http://xml./axis/wsdd/providers/java">
 <handler name="HelloWorldHandler" type="java:axisTest.HelloWorldHandler">
  <parameter name="status" value="success ..."></parameter>
 </handler>
 <service name="HelloWorldWSDDHandler" provider="java:RPC">
  <requestFlow>
   <handler type="HelloWorldHandler"></handler>
  </requestFlow>
  <parameter name="className" value="axisTest.HelloWorldWSDD">
  </parameter>
  <parameter name="allowedMethods" value="*">
  </parameter>
  <parameter name="scope" value="request"></parameter>
  <responseFlow>
   <handler type="HelloWorldHandler"></handler>
  </responseFlow>
 </service>
 
 
<!-- <service name="HelloWorldWSDD" provider="java:RPC">-->
<!--  <parameter name="className" value="axisTest.HelloWorldWSDD" />-->
<!--  <parameter name="allowedMethods" value="*" />-->
<!--  <parameter name="scope" value="request"></parameter>-->
<!-- </service>-->
</deployment>
------------------
HelloWorldHandler.java:
package axisTest;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;

/**服务端:Hander类可以在webservice每个方法被调用之前或后做一些事情
 * @author Tower
 *
 */
public class HelloWorldHandler extends BasicHandler{
 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private long COUNT=0L;
 private int requestCount;
 public void invoke(MessageContext arg0) throws AxisFault {
  requestCount++;
  COUNT++;
  String status=(String)this.getOption("status");
  System.out.println("HelloWorldHandler的status是:"+status+",COUNT="+COUNT+", HandlerRequestCount="+requestCount);
  
 }

}
--------------------
HelloWorldHandlerClient.java:
package axisTest;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

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

/**客户端测试类
 * @author Tower
 *
 */
public class HelloWorldHandlerClient {
public static void main(String[] args) throws ServiceException, RemoteException {
 String url="http://127.0.0.1:8080/axisTest/services/HelloWorldWSDDHandler";
 Service service=new Service();
 Call call=(Call)service.createCall();
 call.setTargetEndpointAddress(url);
 
 call.setOperationName(new QName(url,"hello"));
 String result=(String)call.invoke(new Object[]{"tower888"});
 System.out.println(result);
}
}


控制台客户端输出:
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attach
ment support is disabled.
HELLO: tower888

控制台服务器端输出:
HelloWorldHandler的status是:success ...,COUNT=1, HandlerRequestCount=1
HelloWorldHandler的status是:success ...,COUNT=2, HandlerRequestCount=2
HelloWorldHandler的status是:success ...,COUNT=3, HandlerRequestCount=3
HelloWorldHandler的status是:success ...,COUNT=4, HandlerRequestCount=4

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多