详解spring+webservice接口(axis1方式)

来源:互联网 发布:大唐网络 编辑:程序博客网 时间:2024/05/22 07:58
 

详解spring+webservice接口(axis1方式)

 37人阅读 评论(0) 收藏 举报
 分类:

第一步,添加jar

axis1实现webservice所需jar包 
axis-ant.jar 
axis.jar 

jaxrpc.jar 

spring-remoting.jar

(加粗的为核心包,简单的发布用这四个即可)

commons-discovery-0.2.jar 
commons-logging-1.0.4.jar 

activation.jar 
log4j-1.2.8.jar
mail.jar 
saaj.jar 
wsdl4j-1.5.1.jar 
xalan.jar 
xmlsec-1.2.1.jar

第二步,修改web.xml

[html] view plain copy
  1. <!--axis 需要引入的 Servlet -->   
  2.       <servlet>   
  3.              <servlet-name>AxisServlet</servlet-name>   
  4.              <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>  
  5.       </servlet>   
  6.         <servlet-mapping>   
  7.              <servlet-name>AxisServlet</servlet-name>   
  8.              <url-pattern>/services/*</url-pattern>   
  9.       </servlet-mapping>  

第三步,接口实现类及对外发布的调用方法

webservice.Java(这里getStr() 方法提供给外部调用)

[java] view plain copy
  1. package com.business.apps;  
  2.   
  3. import javax.xml.rpc.ServiceException;  
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.remoting.jaxrpc.ServletEndpointSupport;  
  8.   
  9. import com.business.apps.manager.TestWebService;  
  10.   
  11.   
  12.   
  13. @SuppressWarnings("deprecation")  
  14. public class WebService extends ServletEndpointSupport {  
  15.   
  16.     @Autowired  
  17.     private ApplicationContext applicationContext;  
  18.   
  19.     @Autowired  
  20.     private TestWebService testWebService;  
  21.   
  22.     @Override  
  23.     protected void onInit() throws ServiceException {  
  24.         // 初始化Spirng 配置  
  25.         applicationContext = super.getApplicationContext();  
  26.           
  27.         testWebService = (TestWebService) applicationContext.getBean("testWebService");  
  28.   
  29.     }  
  30.   
  31.       
  32.   
  33.     /** 
  34.      *  
  35.      * @Description: 接口 
  36.      * @param  
  37.      * @return 
  38.      * @throws Exception 
  39.      * @author  
  40.      * @date  
  41.      * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述] 
  42.      */  
  43.   
  44.       
  45.     public String getStr(String jsonString) throws Exception {  
  46.         return String.valueOf(testWebService.getStr(jsonString));  
  47.     }  
  48. }  

上面接口涉及的两个处理类如下:

TestWebserviceImpl.java

[java] view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package com.business.apps.manager.impl;  
  5.   
  6. /** 
  7.  * @author  
  8.  * @version 创建时间:2017年1月5日 下午4:21:25  
  9.  *  
  10.  */  
  11.   
  12. import java.sql.Date;  
  13. import java.sql.Timestamp;  
  14. import java.util.HashMap;  
  15. import java.util.Map;  
  16.   
  17. import javax.jws.WebMethod;  
  18. import javax.jws.WebResult;  
  19. import javax.jws.WebService;  
  20.   
  21. import net.sf.json.JSONObject;  
  22.   
  23. import org.apache.commons.lang.StringUtils;  
  24. import org.dom4j.Document;  
  25. import org.dom4j.DocumentHelper;  
  26. import org.dom4j.Element;  
  27. import org.springframework.beans.factory.annotation.Autowired;  
  28. import org.springframework.stereotype.Component;  
  29. import org.springframework.transaction.annotation.Transactional;  
  30. import com.business.apps.manager.TestWebService;  
  31. public class TestWebServiceImpl implements TestWebService{  
  32. @SuppressWarnings("null")  
  33. @Transactional  
  34.     public String getStr(String jsonString)throws Exception {///接收参数为String,然后再返回一个字符串String,方法里面是Json处理  
  35.         JSONObject jsonOb = JSONObject.fromObject(jsonString);  
  36.         String serviceTicket = jsonOb.optString("serviceTicket");   
  37.         Map<String, String> result = new HashMap<String, String>();  
  38.         result.put("URL","http://www.baidu.com");  
  39.         JSONObject json = JSONObject.fromObject(result);  
  40.         return json.toString();  
  41.     }     
  42.   
  43. }  
  44. /*@Component //这里是注解的方式发布接口,配置文件需要配置注解机制,此处略 
  45. @WebService(serviceName = "/TestService", targetNamespace = "http://localhost:8080") 
  46. public class TestWebServiceImpl implements TestWebService{ 
  47.      
  48.     @WebMethod 
  49.     @WebResult(targetNamespace = "http://localhost:8080") 
  50.     @Transactional 
  51.     @Override 
  52.     public String getWgceaUrl(String jsonString) { 
  53.         JSONObject jsonOb = JSONObject.fromObject(jsonString); 
  54.         String serviceTicket = jsonOb.optString("serviceTicket");  
  55.         Map<String, String> result = new HashMap<String, String>(); 
  56.         result.put("URL","http://localhost:8080/*************"); 
  57.         JSONObject json = JSONObject.fromObject(result); 
  58.         return json.toString(); 
  59.     } 
  60.      
  61. }*/  

TestWebservice.java(TestWebserviceImpl对应的接口)


[java] view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package com.business.apps.manager;  
  5.   
  6. /** 
  7.  * @author  
  8.  * @version 创建时间:2017年1月5日 下午4:22:45  
  9.  *  
  10.  */  
  11.   
  12. import javax.jws.WebMethod;  
  13. import javax.jws.WebParam;  
  14. import javax.jws.WebResult;  
  15. import javax.jws.WebService;  
  16. import javax.xml.ws.BindingType;  
  17. import javax.xml.ws.soap.SOAPBinding;  
  18.   
  19. public interface TestWebService {  
  20.   
  21.     public String getWgceaUrl(String jsonString)throws Exception;  
  22.   
  23. }  

第四步,创建配置文件:在WEB-INF下新建文件“server-config.wsdd”

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <deployment    
  3.    xmlns="http://xml.apache.org/axis/wsdd/"    
  4.     xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">    
  5.    <globalConfiguration>    
  6.        <parameter  name="adminPassword"  value="admin" />    
  7.        <parameter  name="sendXsiTypes"  value="true" />    
  8.        <parameter  name="sendMultiRefs"  value="true" />    
  9.        <parameter  name="sendXMLDeclaration"  value="true" />    
  10.        <parameter  name="axis.sendMinimizedElements"  value="true" />    
  11.         <requestFlow>    
  12.             <handler  type="java:org.apache.axis.handlers.JWSHandler">    
  13.                 <parameter  name="scope"  value="session" />    
  14.             </handler>    
  15.             <handler  type="java:org.apache.axis.handlers.JWSHandler">    
  16.                <parameter  name="scope"  value="request" />    
  17.                 <parameter  name="extension"  value=".jwr" />    
  18.            </handler>    
  19.         </requestFlow>    
  20.     </globalConfiguration>    
  21.     <handler  name="Authenticate"    
  22.   
  23. type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />    
  24.     <handler  name="LocalResponder"    
  25.   
  26. type="java:org.apache.axis.transport.local.LocalResponder" />    
  27.     <handler  name="URLMapper"  type="java:org.apache.axis.handlers.http.URLMapper"   
  28.   
  29. />    
  30.     <service  name="AdminService"  provider="java:MSG">    
  31.         <parameter  name="allowedMethods"  value="AdminService" />    
  32.         <parameter  name="enableRemoteAdmin"  value="false" />    
  33.        <parameter  name="className"  value="org.apache.axis.utils.Admin" />    
  34.         <namespace>http://xml.apache.org/axis/wsdd/</namespace>    
  35.     </service>    
  36.    <service  name="Version"  provider="java:RPC">    
  37.        <parameter  name="allowedMethods"  value="getVersion" />    
  38.        <parameter  name="className"  value="org.apache.axis.Version" />    
  39.     </service>    
  40.     <transport  name="http">    
  41.         <requestFlow>    
  42.             <handler  type="URLMapper" />    
  43.            <handler  type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />    
  44.         </requestFlow>    
  45.     </transport>    
  46.    <transport  name="local">    
  47.         <responseFlow>    
  48.            <handler  type="LocalResponder" />    
  49.        </responseFlow>    
  50.     </transport>    
  51.     
  52.     <!-- 自定义服务 -->    
  53.     <service  name="WebService"  provider="java:RPC">    
  54.        <parameter  name="className"  value="com.business.apps.WebService" />    
  55.    </service>    
  56. </deployment>    

第五步、sping配置文件(Spring-axis.xml)配置bean(项目位置/WEB-INF/config/context/Spring-axis.xml)


[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  4.                                              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  5.                                              http://www.springframework.org/schema/context  
  6.                                              http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  7.       
  8.   
  9.       <bean   id="testWebService"   class="com.business.apps.manager.impl.TestWebServiceImpl" />    
  10.   
  11.   
  12.       
  13. </beans>  





第六步、web.xml里配置sping容器

[html] view plain copy
  1. <!-- spring容器配置 -->  
  2.     <listener>  
  3.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4.     </listener>  
  5.     <context-param>  
  6.         <param-name>contextConfigLocation</param-name>  
  7.         <param-value>           
  8.             /WEB-INF/config/context/Spring-axis.xml  
  9.         </param-value>  
  10.     </context-param>  
  11.   
  12.     <listener>  
  13.         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  14.     </listener>  

第七步、调试无错误后之后启动eclipse,在浏览器里输入 http://localhost:8080/services/WebService?wsdl 有xml显示后编写调用接口类

testWebServiceGet.java

[java] view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package com.business.apps;  
  5.   
  6. /** 
  7.  * @author  
  8.  * @version 创建时间:2017年1月5日 下午5:35:26  
  9.  *  
  10.  */  
  11.   
  12. import java.net.URL;  
  13. import java.util.HashMap;  
  14. import java.util.Map;  
  15. import java.util.Random;  
  16.   
  17. import javax.xml.namespace.QName;  
  18.   
  19. import net.sf.json.JSONObject;  
  20.   
  21. import org.apache.axis.client.Call;  
  22. import org.apache.axis.client.Service;  
  23.   
  24.   
  25. public class testWebServiceGet {  
  26.   
  27.     private String nameSpaceUri = "http://localhost:8080/services/WebService";  
  28.     //private String nameSpaceUri = "http://www.ntrl.gov.cn/services/WebService";  
  29.     private String wsdlUrl = nameSpaceUri + "?wsdl";  
  30.   
  31.     private Service service;  
  32.     private Call call;  
  33.   
  34.     public final void init() throws Exception {  
  35.         // 创建调用对象  
  36.         service = new Service();  
  37.         call = (Call) service.createCall();  
  38.   
  39.         // 调用 远程方法  
  40.         call.setOperationName(new QName(nameSpaceUri, "getStr"));  
  41.         // 设置URL  
  42.         call.setTargetEndpointAddress(new URL(nameSpaceUri));  
  43.     }  
  44.   
  45.   
  46.       
  47.     public final void testGet() throws Exception {  
  48.   
  49.    
  50.         Map<String, String> result = new HashMap<String, String>();  
  51.         result.put("urlInput","http://www.google.com");  
  52.         JSONObject json = JSONObject.fromObject(result);  
  53.         String jsonString =json.toString();                               
  54.   
  55.         // 执行远程调用,同时获得返回值  
  56.         String r = (String) call.invoke(new Object[] {jsonString});  
  57.         JSONObject jsons = JSONObject.fromObject(r);  
  58.         String jsonStrings =jsons.toString();     
  59.         System.out.println("jsonStrings=" + jsonStrings);  
  60.   
  61.     }  
  62.   
  63.   
  64.     public static void main(String[] args) {  
  65.           
  66.         testWebServiceGet test = new testWebServiceGet();  
  67.     try {  
  68.             test.init();  
  69.             test.testGet();  
  70.         } catch (Exception e) {  
  71.             e.printStackTrace();  
  72.     }  
  73.     }  
  74.   
  75.       
  76.   
  77. }  
0 0