webservice 入门案例

最近准备考试:将webservice 充充电,本想配置一遍,在网上资料,资源虽多,没有几个能成功的,还没有几个有原代码的,可能是自己理解能力有限,摸索了一段时间,终于调通了webservice !

    在此为想学习webservice同仁们提供一个入门例子

 

 1、新建一个web project,如下面的例子为Storm

 2、在工程中目录如下图所示

 

Storm.
│  .classpath
│  .project
│  index.html
│  tree.txt
│  
├─.settings
│      com.spket.js.prefs
│      org.eclipse.core.resources.prefs
│      
├─src
│  └─com
│      │  TestDWR.java
│      │  TestWebService.java
│      │  
│      └─service
│              HelloWorld.java
│              
└─WEB-INF
    │  server-config.wsdd
    │  web.xml
    │  
    ├─attachments
    ├─classes
    │  └─com
    │      │  TestDWR.class
    │      │  TestWebService.class
    │      │  
    │      └─service
    │              HelloWorld.class
    │              
    └─lib
            axis-ant.jar
            axis.jar
            commons-discovery-0.2.jar
            commons-logging-1.0.4.jar
            jaxrpc.jar
            log4j-1.2.8.jar
            saaj.jar
            wsdl4j-1.5.1.jar

3、编写java类

Java代码 复制代码  收藏代码
  1. public class HelloWorld {   
  2.   
  3.     private String name = "qingfeng";   
  4.        
  5.     public String getName(){   
  6.         return name;   
  7.     }   
  8.     public String hiHello(String name)   
  9.     {   
  10.         return "Hi:" + name;   
  11.     }   
  12. }  
public class HelloWorld {

	private String name = "qingfeng";
	
	public String getName(){
		return name;
	}
	public String hiHello(String name)
	{
		return "Hi:" + name;
	}
}

 

4、修改web.xml

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app  xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">  
  3.   
  4.     <servlet>  
  5.         <servlet-name>AxisServlet</servlet-name>  
  6.         <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>  
  7.     </servlet>  
  8.     <servlet-mapping>  
  9.         <servlet-name>AxisServlet</servlet-name>  
  10.         <url-pattern>/services/*</url-pattern>  
  11.     </servlet-mapping>  
  12.        
  13.   <welcome-file-list>  
  14.     <welcome-file>index.jsp</welcome-file>  
  15.     <welcome-file>index.html</welcome-file>  
  16.   </welcome-file-list>  
  17. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app  xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

    <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>index.html</welcome-file>
  </welcome-file-list>
</web-app>

 5、修改 server-config.wsdd文件,与web.xml同一级目录,内容如下,在别面拷贝一个,修改自己类及方法

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">     
  3.   
  4. <globalConfiguration>  
  5.   <parameter name="sendMultiRefs" value="true"/>  
  6.   <parameter name="disablePrettyXML" value="true"/>  
  7.   <parameter name="dotNetSoapEncFix" value="true"/>  
  8.   <parameter name="enableNamespacePrefixOptimization" value="false"/>  
  9.   <parameter name="sendXMLDeclaration" value="true"/>  
  10.   <parameter name="sendXsiTypes" value="true"/>  
  11.   <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>  
  12. </globalConfiguration>  
  13.   
  14.   <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>     
  15.      
  16.    <service name="hello" provider="java:RPC">    
  17.       <parameter name="className" value="com.service.HelloWorld" />    
  18.       <parameter name="allowedMethods" value="*" />    
  19.    </service>    
  20.      
  21.    <service name="test" provider="java:RPC">    
  22.       <parameter name="className" value="com.TestDWR" />    
  23.       <parameter name="allowedMethods" value="*" />    
  24.    </service>     
  25.       
  26.   <transport name="http">     
  27.      <requestFlow>     
  28.         <handler type="URLMapper"/>     
  29.      </requestFlow>     
  30.   </transport>     
  31. </deployment>  
<?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="hello" provider="java:RPC"> 
      <parameter name="className" value="com.service.HelloWorld" /> 
      <parameter name="allowedMethods" value="*" /> 
   </service> 
  
   <service name="test" provider="java:RPC"> 
      <parameter name="className" value="com.TestDWR" /> 
      <parameter name="allowedMethods" value="*" /> 
   </service>  
   
  <transport name="http">  
     <requestFlow>  
        <handler type="URLMapper"/>  
     </requestFlow>  
  </transport>  
</deployment>

6、将相关的jar加上,如上述目录所示,将工程布置到tomcat中,

7、访问发布的路径,如我本地为:http://localhost:8080/Storm/services

8、若在IE中,看到如下所示,就表明成功了

 

And now... Some Services

hello (wsdl)

  • helloName
  • getName

test  (wsdl)

  • getAge

9、下面我们再写一个测试类,提供外界访问,在工程目录中com.TestWebService类,代码如下所示

Java代码 复制代码  收藏代码
  1. package com;   
  2.   
  3. import javax.xml.rpc.ServiceException;   
  4.   
  5. import org.apache.axis.client.Call;   
  6. import org.apache.axis.client.Service;   
  7.   
  8. public class TestWebService {   
  9.   
  10.     public static void test2() {   
  11.         String url = "http://localhost:8080/Storm/services/hello";   
  12.         Service service = new Service();   
  13.         Call call;   
  14.         String name = "东东和范范";   
  15.         try {   
  16.             call = (Call) service.createCall();   
  17.             call.setTargetEndpointAddress(new java.net.URL(url));   
  18.             call.setOperationName("helloName");   
  19.             String res = (String) call.invoke(new Object[] { name });   
  20.             System.out.println(res);   
  21.         } catch (ServiceException e) {   
  22.             e.printStackTrace();   
  23.         } catch (Exception e) {   
  24.             e.printStackTrace();   
  25.         }   
  26.     }   
  27.     /**  
  28.      * @param args  
  29.      */  
  30.     public static void main(String[] args) {   
  31.         test2();   
  32.     }   
  33.   
  34. }  
package com;

import javax.xml.rpc.ServiceException;

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

public class TestWebService {

	public static void test2() {
		String url = "http://localhost:8080/Storm/services/hello";
		Service service = new Service();
		Call call;
		String name = "东东和范范";
		try {
			call = (Call) service.createCall();
			call.setTargetEndpointAddress(new java.net.URL(url));
			call.setOperationName("helloName");
			String res = (String) call.invoke(new Object[] { name });
			System.out.println(res);
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		test2();
	}

}

 10、在控制台输出【hello:东东和范范】表明成功。

 11、附上整个项目源代码

 

你可能感兴趣的:(java,apache,xml,webservice,servlet)