欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

webservice 简单的例子 WebServiceWebXMLApache.net

程序员文章站 2022-03-02 16:56:07
...
把jar下载下来,放到web工程里,然后创建一个类如下:
public class Login {
public String getname(){
return "hahah";
}
}
然后再跟web.xml同一个位置创建一个
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="sendMultiRefs" value="true"/>
  <parameter name="disablePrettyXML" value="true"/>
  <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"/>
</globalConfiguration>
<handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/> 
  <service name="Login.jws" provider="java:RPC"> 
     <parameter name="className" value="com.**.Login"/>// 你要改的就是这.这写上要调用的类.就可以
     <parameter name="scope" value="request"/>
     <parameter name="allowedMethods" value="*"/>
     <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
  </service> 
  <transport name="http"> 
     <requestFlow> 
        <handler type="URLMapper"/> 
     </requestFlow> 
  </transport> 
</deployment>

然后再web.xml中添加:
<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>
然后再写一个测试类:
String url = "http://localhost:8080/webservice/services/Login.jws";//         
Service service = new Service();
        Call call;
        try {
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(url) );
            call.setOperationName("getname" );// 这是要调用的方法
             String res = (String) call.invoke( new Object[] {} );
            System.out.println(res);
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
即可,webervice简单的列子,运行测试一下就可以!