1.pom.xml配置
<cxf.version>3.1.7</cxf.version>
<!-- cxf.dependency -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
2.web.xml
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
3.mybatis.xml
---xmlns:jaxws="http://cxf.apache.org/jaxws"
--http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 扫描cxf包 -->
<context:component-scan base-package="com.djzh.cxf"></context:component-scan>
<!-- cxf整合spring -->
<jaxws:endpoint implementor="#query" address="/query" />
4.interface
@WebService
public interface CXFInterface {
String testConnection(String key);
String GetXJGH(int DWID,int JC,String Key);
String getXJDW(String ZH,String KHMC,String Key);
String getLastTime(int dwid,String Key);
String putLastTime(int DWID,String Date,String Key);
String GetPZS(int DWID,String Date1,String Date2,int XZED,String Key);
}
package com.djzh.cxf.impl;
import java.util.List;
import javax.annotation.Resource;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.djzh.basicdata.dao.IGhdmDao;
import com.djzh.cxf.CXFInterface;
import com.djzh.entity.Ghdm;
import com.djzh.entity.GhdmExample;
@Component("query")
@WebService
public class CXFInterfaceImpl implements CXFInterface {
private static String KEY="1";
@Resource
IGhdmDao ghdmDao;
@Override
public String testConnection(String key) {
if(KEY.equals(key)) {
return "CONNECTED";
}else {
throw new RuntimeException("key不相符");
}
}
@Override
public String GetXJGH(int dwid, int jc, String Key) {
//1.查询数据
GhdmExample ghdmExample=new GhdmExample();
ghdmExample.createCriteria().andSjghdmEqualTo(dwid+"").andGhccEqualTo(jc+"");
List<Ghdm> ghdmList=ghdmDao.selectByExample(ghdmExample);
//2.解析xml
String dws=getXml(ghdmList);
return dws;
}
private String getXml(List<Ghdm> ghdmList) {
if(ghdmList != null && ghdmList.size()>0) {
StringBuilder ghdmXmlStr = new StringBuilder();
ghdmXmlStr.append("<DWS>");
for(Ghdm ghdm:ghdmList) {
ghdmXmlStr.append("<DW><ID>"+ghdm.getGhdm()+"</ID><GHMC>"+ghdm.getGhmc()+"</GHMC><DW>");
}
ghdmXmlStr.append("</DWS>");
return ghdmXmlStr.toString();
}else {
return "";
}
}
@Override
public String getLastTime(int dwid, String Key) {
String lastTime=ghdmDao.getLastDownLoadTime(dwid);
return "<Res><LASTDATE>"+(lastTime==null?"":lastTime)+"</LASTDATE></Res>";
}
@Override
public String getXJDW(String ZH, String KHMC, String Key) {
// TODO Auto-generated method stub
return null;
}
@Override
public String putLastTime(int dwid, String date, String Key) {
ghdmDao.deleteLastDownLoadTime(dwid);
int insertFlag = ghdmDao.insertLastDownLoadTime(dwid,date);
if(insertFlag>0) {
return "<Res>OK</Res>";
}else {
return "上传失败";
}
}
@Override
public String GetPZS(int DWID, String Date1, String Date2, int XZED, String Key) {
// TODO Auto-generated method stub
return null;
}
}
原文地址:https://www.cnblogs.com/523823-wu/p/8744845.html