SpringMVC 与axis2 的整合(服务器端)

1,新建一个web project项目,项目的目录 如下:

2,导入需要的jar包,本例用的是axis2 1.7.3版本。

3,创建接口。

package com.dsdl.hcm.webService.service;

import java.util.List;

import com.dsdl.hcm.businessmanagement.bean.HcmEmptyboxBean;



public interface EmptyBoxService {

	public String fanhui(String s);
	public int updateHcmEmptyBoxForBoxCodeBySano(List<HcmEmptyboxBean> hcmEmptyboxBeanList);
	public int getPrice();
	public String getGreeting(String name);
	public String recieveXml(String str);
}

4,创建接口的实现类EmptyBoxServiceImpl

package com.dsdl.hcm.webService.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.dsdl.hcm.businessmanagement.bean.HcmEmptyboxBean;
import com.dsdl.hcm.businessmanagement.dao.HcmEmptyboxDao;
import com.dsdl.hcm.webService.bean.HcmEmptyboxPojo;
import com.dsdl.hcm.webService.service.EmptyBoxService;
import com.dsdl.hcm.webService.xmlUtil.Xml;
import com.dsdl.hcm.webService.xmlUtil.XmlHead;
import com.dsdl.hcm.webService.xmlUtil.XmlUtil;

public class EmptyBoxServiceImpl implements EmptyBoxService {

	@Resource(name="hcmEmptyboxDao")
	public HcmEmptyboxDao hcmEmptyboxDao;
	
	@Override
	public String fanhui(String s) {
		// TODO Auto-generated method stub
		return s+""+hcmEmptyboxDao.toString();
	}

	@Override
	public int updateHcmEmptyBoxForBoxCodeBySano(
			List<HcmEmptyboxBean> hcmEmptyboxBeanList) {
		// TODO Auto-generated method stub
		return hcmEmptyboxDao.updateHcmEmptyBoxForBoxCodeBySano(hcmEmptyboxBeanList);
	}

	@Override
	public int getPrice() {
		// TODO Auto-generated method stub
		System.out.println(hcmEmptyboxDao+"================");
		return 11;
	}

	@Override
	public String getGreeting(String name) {
		// TODO Auto-generated method stub
		return "你好 " + name;
	}
	@Override
	public String recieveXml(String str){
		try {
			Xml xml = new Xml();
			Xml xmls = (Xml) XmlUtil.getObjectFromXml(xml, str);
			System.out.println(xmls.getXMLHEAD().getREQUESTUSER());
			
			switch (xmls.getXMLHEAD().getREQUESTMETHOD()) {
			case "writebackboxcode":
				return this.writeBackBoxCode(xmls);
			default:
				return this.writeBackBoxCode(xmls);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "异常";
		}
	}
	
	private String writeBackBoxCode(Xml xmls) {
		// TODO Auto-generated method stub
//	    BusinessManagementController businessManagementController = (com.dsdl.hcm.businessmanagement.controller.BusinessManagementController) ctx.getBean("businessManagementController");
		List<HcmEmptyboxPojo> HcmEmptyboxPojo = xmls.getXMLBODY().getHCMEMPTYBOXPOJOLIST();
		List<HcmEmptyboxBean> hcmEmptyboxBeanList = new ArrayList<HcmEmptyboxBean>();
		HcmEmptyboxBean hcmEmptyboxBean = null;
		
		for (HcmEmptyboxPojo temp : HcmEmptyboxPojo) {
			hcmEmptyboxBean = new HcmEmptyboxBean();
			hcmEmptyboxBean.setSano(temp.getSANO());
			hcmEmptyboxBean.setBoxCode(temp.getBOXCODE());
			hcmEmptyboxBeanList.add(hcmEmptyboxBean);
		}
		int count = hcmEmptyboxDao.updateHcmEmptyBoxForBoxCodeBySano(hcmEmptyboxBeanList);
		Xml resultXml = new Xml();
		XmlHead xmlHead = new XmlHead();
		xmlHead.setREQUESTTIME("");
		xmlHead.setREQUESTUSER("");
		if(count > 0){
			xmlHead.setSTATUS(1);
			xmlHead.setSTATUS_TEXT("成功");
		}else{
			xmlHead.setSTATUS(0);
			xmlHead.setSTATUS_TEXT("失败");
		}
		resultXml.setXMLHEAD(xmlHead);
		return XmlUtil.getXmlFromObject(resultXml);
	}
}

5,webservice类SimpleService

package com.dsdl.hcm.webService.server;
import java.util.List;
import org.apache.axis2.AxisFault;
import org.apache.axis2.ServiceObjectSupplier;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.i18n.Messages;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.dsdl.hcm.businessmanagement.bean.HcmEmptyboxBean;
import com.dsdl.hcm.webService.service.EmptyBoxService;

public class SimpleService implements EmptyBoxService {
	private static ApplicationContext ctx;
			
	@Override
	public String fanhui(String s) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public int updateHcmEmptyBoxForBoxCodeBySano(
			List<HcmEmptyboxBean> hcmEmptyboxBeanList) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public int getPrice() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public String getGreeting(String name) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String recieveXml(String str) {
		// TODO Auto-generated method stub
		return null;
	}
	


}

6,web.xml

  <servlet>    
        <servlet-name>AxisServlet</servlet-name>    
        <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>    
        <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:conf/axis2.xml</param-value>
		</init-param>
        <load-on-startup>1</load-on-startup>    
    </servlet>    
            
    <servlet-mapping>    
        <servlet-name>AxisServlet</servlet-name>    
        <url-pattern>/services/*</url-pattern>    
    </servlet-mapping> 

7, 所使用的框架是SSM,在配置文件中spring-mybatis.xml 中配置。

<bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
<bean id="emptyBoxService" class="com.dsdl.hcm.webService.service.impl.EmptyBoxServiceImpl"></bean>

 在这个里面配置,主要取决于,web.xml的配置

8,WEB-INF\services\XXX\META-INF\services.xml,在此目录下配置service.xml ,如果不存在则自己创建。其中XXX可以自己定义。

<serviceGroup>
<service name="SimpleService" >
    <description>  
      	Test Service Example   
    </description>  
    <parameter name="ServiceObjectSupplier"><!-- 固定的不变 -->  
            org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier  
        </parameter>  
    <parameter name="SpringBeanName">emptyBoxService</parameter><!-- bean名字 -->  
    <parameter name="ServiceClass">  
        com.dsdl.hcm.webService.server.SimpleService
    </parameter>  
   <messageReceivers>
   	 	<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
       	<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </messageReceivers>  
</service>
</serviceGroup>

  到此为止,axis2+springMVC集成到此已经开发完成,接下把工程部署到tomcat。

  访问地址是http://localhost:8080/HCM/services/SimpleService?wsdl。

  本人亲测,如有不当之处,敬请批评指正,共同学习,共同进步。

 

posted @ 2016-09-23 15:32  流浪小妮子  阅读(6783)  评论(0编辑  收藏  举报