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

第一步,添加jar

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

jaxrpc.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

	 
        
              AxisServlet 
              org.apache.axis.transport.http.AxisServlet
        
  		 
              AxisServlet 
              /services/* 
       
	

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

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

package com.business.apps;

import javax.xml.rpc.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;

import com.business.apps.manager.TestWebService;



@SuppressWarnings("deprecation")
public class WebService extends ServletEndpointSupport {

	@Autowired
	private ApplicationContext applicationContext;

	@Autowired
	private TestWebService testWebService;

	@Override
	protected void onInit() throws ServiceException {
		// 初始化Spirng 配置
		applicationContext = super.getApplicationContext();
		
		testWebService = (TestWebService) applicationContext.getBean("testWebService");

	}

	

	/**
	 * 
	 * @Description: 接口
	 * @param 
	 * @return
	 * @throws Exception
	 * @author 
	 * @date 
	 * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */

	
	public String getStr(String jsonString) throws Exception {
		return String.valueOf(testWebService.getStr(jsonString));
	}
}

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

TestWebserviceImpl.java

/**
 * 
 */
package com.business.apps.manager.impl;

/**
 * @author 
 * @version 创建时间:2017年1月5日 下午4:21:25 
 * 
 */

import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.business.apps.manager.TestWebService;
public class TestWebServiceImpl implements TestWebService{
@SuppressWarnings("null")
@Transactional
	public String getStr(String jsonString)throws Exception {///接收参数为String,然后再返回一个字符串String,方法里面是Json处理
		JSONObject jsonOb = JSONObject.fromObject(jsonString);
		String serviceTicket = jsonOb.optString("serviceTicket"); 
		Map result = new HashMap();
		result.put("URL","http://www.baidu.com");
		JSONObject json = JSONObject.fromObject(result);
		return json.toString();
	}	

}
/*@Component //这里是注解的方式发布接口,配置文件需要配置注解机制,此处略
@WebService(serviceName = "/TestService", targetNamespace = "http://localhost:8080")
public class TestWebServiceImpl implements TestWebService{
	
	@WebMethod
	@WebResult(targetNamespace = "http://localhost:8080")
	@Transactional
	@Override
	public String getWgceaUrl(String jsonString) {
		JSONObject jsonOb = JSONObject.fromObject(jsonString);
		String serviceTicket = jsonOb.optString("serviceTicket"); 
		Map result = new HashMap();
		result.put("URL","http://localhost:8080/*************");
		JSONObject json = JSONObject.fromObject(result);
		return json.toString();
	}
	
}*/

TestWebservice.java(TestWebserviceImpl对应的接口)


/**
 * 
 */
package com.business.apps.manager;

/**
 * @author 
 * @version 创建时间:2017年1月5日 下午4:22:45 
 * 
 */

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

public interface TestWebService {

	public String getWgceaUrl(String jsonString)throws Exception;

}

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

  
  
     
         
         
         
         
         
          
              
                  
              
              
                 
                  
             
          
      
      
      
      
      
          
          
         
        http://xml.apache.org/axis/wsdd/  
      
     
         
         
      
      
          
              
             
          
      
     
          
             
         
      
  
      
      
         
     
  

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




	

        


	





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


	
		org.springframework.web.context.ContextLoaderListener
	
	
		contextConfigLocation
					
			/WEB-INF/config/context/Spring-axis.xml
		
	

	
		org.springframework.web.util.IntrospectorCleanupListener
	

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

testWebServiceGet.java

/**
 * 
 */
package com.business.apps;

/**
 * @author 
 * @version 创建时间:2017年1月5日 下午5:35:26 
 * 
 */

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.xml.namespace.QName;

import net.sf.json.JSONObject;

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


public class testWebServiceGet {

    private String nameSpaceUri = "http://localhost:8080/services/WebService";
	//private String nameSpaceUri = "http://www.ntrl.gov.cn/services/WebService";
	private String wsdlUrl = nameSpaceUri + "?wsdl";

	private Service service;
	private Call call;

	public final void init() throws Exception {
		// 创建调用对象
		service = new Service();
		call = (Call) service.createCall();

		// 调用 远程方法
		call.setOperationName(new QName(nameSpaceUri, "getStr"));
		// 设置URL
		call.setTargetEndpointAddress(new URL(nameSpaceUri));
	}


	
	public final void testGet() throws Exception {

 
		Map result = new HashMap();
		result.put("urlInput","http://www.google.com");
		JSONObject json = JSONObject.fromObject(result);
		String jsonString =json.toString();								

		// 执行远程调用,同时获得返回值
		String r = (String) call.invoke(new Object[] {jsonString});
		JSONObject jsons = JSONObject.fromObject(r);
		String jsonStrings =jsons.toString();	
		System.out.println("jsonStrings=" + jsonStrings);

	}


	public static void main(String[] args) {
		
		testWebServiceGet test = new testWebServiceGet();
	try {
			test.init();
		    test.testGet();
		} catch (Exception e) {
			e.printStackTrace();
	}
	}

	

}














你可能感兴趣的:(java)