SpringMVC 与axis2 的整合(服务器端)
阅读原文时间:2023年07月13日阅读:2

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 = xmls.getXMLBODY().getHCMEMPTYBOXPOJOLIST();
List hcmEmptyboxBeanList = new ArrayList();
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

AxisServlet org.apache.axis2.transport.http.AxisServlet contextConfigLocation classpath:conf/axis2.xml 1

<servlet-mapping>  
    <servlet-name>AxisServlet</servlet-name>  
    <url-pattern>/services/\*</url-pattern>  
</servlet-mapping>

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


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

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

Test Service Example org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier emptyBoxService com.dsdl.hcm.webService.server.SimpleService

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

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

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