当前位置: 代码迷 >> Web前端 >> 一个简略的webservice构建
  详细解决方案

一个简略的webservice构建

热度:59   发布时间:2012-11-08 08:48:12.0
一个简单的webservice构建
一个简简单单的webservice例子。可以编写测试类,也可以在浏览器输入http://localhost:8989/webservice/services测试webservice是否配置成功。

后台方法:
package com.yyn;
/**
* 测试方法
* @author Andy
* */
public class Login {
    public String Add(String str){
        return "yyn"+str;
    }

}
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.yyn.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文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.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>
<servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/servlet/AxisServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
  <welcome-file>
  index.jsp
  </welcome-file>
  </welcome-file-list>
</web-app>

测试客户端:
package com.yyn;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
/**
* @author Andy
* */
public class TestClient {  
        public static void main(String[] args) throws Exception {  
        String url = "http://localhost:8989/webservice/services/Login.jws";    
        Service service = new Service();
            Call call;
            try {
                call = (Call) service.createCall();
                call.setTargetEndpointAddress(new java.net.URL(url) );
                call.setOperationName("Add" );// 这是要调用的方法
                 String res = (String) call.invoke( new Object[]{"boy!"} );
                System.out.println(res);
            } catch (ServiceException e) {
                e.printStackTrace();
            }
    }
    }

  相关解决方案