—注解:@WebService(SEI 和 SEI 的实现类)
—注解:@WebMethod(SEI中的所有方法)
lib包中放入jar包
选择which上面和下面的jar包,build path
WEB-INF 放入web.xml
放入applicationContext.xml
创建接口
@WebService(targetNamespace="http://www.neusoft.com/")
public interface WsHello{
public String sayHello(String xmldata);
}
创建实现类
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import com.xxx.testWS.WsHello;
@WebService(targetNamespace="http://www.xxx.com/",endpointInterface="com.xxx.testWS.WSHello",serviceName="hello")
@BindingType(value=javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public class WsHelloImpl implements WsHello{
@WebMethod
public String sayHello(String xmlData) {
return null;
}
}
修改applicationContext.xml
创建启动类
import javax.xml.ws.Endpoint;
import com.xxx.service.referrals.impl.ReferralsServiceImpl;
public class qdTest {
public static void main(String[] args) {
String address = "http://localhost:8890/router/ws?wsdl";
Endpoint.publish(address, new ReferralsServiceImpl());
System.out.println("发布成功");
}
}
调用类
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import com.xxx.service.referrals.ReferralsService;
public class test {
public static void main(String[] args) {
// 初始化代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
// 定义服务接口类型
factory.setServiceClass(ReferralsService.class);
// 设置服务地址
factory.setAddress("http://localhost:8890/router/ws?wsdl");
ReferralsService client = (ReferralsService) factory.create();
try {
//读取xml文件
SAXReader reader = new SAXReader();
//取得xml内容转为document对象
Document document = reader.read("e:\\1.xml");
//将document对象转换为String
String xml = document.asXML();
//调用业务
client.getReferralsInfoByIdCard(xml);
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行到webservice实体类中 调用dao
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import com.xxx.dao.referrals.GetReferralsInfo;
@WebService(targetNamespace="http://www.xxx.com/",endpointInterface="com.xxx.service.referrals.ReferralsService",serviceName="referrals")
@BindingType(value=javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public class ReferralsServiceImpl {
public String getReferralsInfoByIdCard(String xmldata) {
String reqXml = null;
GetReferralsInfo gri = new GetReferralsInfo();
reqXml = gri.getReferralsList(xmldata);
return reqXml;
}
}
dao层(需要用到SwitchsetXml,(获取前后部分),Referrals(实体类),C3P0Utils(获取数据库连接))
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.neusoft.assemble.SwitchsetXml;
import com.xxx.tableBean.Referrals;
import com.xxx.util.C3P0Utils;
public class GetReferralsInfo {
//接数据的实体类对象
Referrals referrals = new Referrals();
@SuppressWarnings("deprecation")
public String getReferralsList(String xmldata){//获取到传来的 str 入参xml
Document docXml = null;
//拼接xml标签字符串的工具类
SwitchsetXml switchset = new SwitchsetXml();
//身份证号
String idCardNumValue = null;
QueryRunner qr = new QueryRunner();
//获取数据库链接
Connection conn = C3P0Utils.getConnection();
//xml文件
StringBuffer str = new StringBuffer();
//xml前部分文件
StringBuffer str1 = new StringBuffer();
//xml后部分文件
StringBuffer str2 = new StringBuffer();
try {
//把str的xml入参 再转成document类型
docXml = DocumentHelper.parseText(xmldata);
} catch (DocumentException e) {
e.printStackTrace();
}
//调用SwitchsetXml</standardcode>之前的前部分xml
str1 = switchset.assembleXmlBefore(docXml);
//获取根节点
Element rootNode = docXml.getRootElement();
//获取跟节点下的business节点
Element businessNode = rootNode.element("business");
Element businessdataNode = businessNode.element("businessdata");
Element datasetsNode = businessdataNode.element("datasets");
Element setdetailsNode = datasetsNode.element("setdetails");
idCardNumValue = setdetailsNode.element("cert_num").getText();
System.out.println("获取到身份证号"+idCardNumValue);
String sql1 = "select * from REFERRALS where cert_num = ?";
Object[] parm = {idCardNumValue};
List<Map<String, Object>> list1;
try {
list1 = qr.query(conn, sql1, parm, new MapListHandler());
if (list1.size() < 0 || list1.isEmpty()) {
}else{
try {
//获取到查询数据放入referrals中
BeanUtils.populate(referrals, list1.get(0));
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
//xml前部分
str.append(str1);
//拼接中间带查询数据的部分
str.append("<datacompress>0</datacompress>");
str.append("<businessdata>");
str.append("<dmp>");
str.append("<datasets>");
str.append("<setcode/>");
str.append("<settype/>");
str.append("<setdetails>");
str.append("<certtype_code>");
str.append(referrals.getCERTTYPE_CODE());
str.append("</certtype_code>");
str.append("<certtype_name>");
str.append(referrals.getCERTTYPE_NAME());
str.append("</certtype_name>");
str.append("<cert_num>");
str.append(referrals.getCERT_NUM());
str.append("</cert_num>");
str.append("<name>");
str.append(referrals.getSELF_NAME());
str.append("</name>");
str.append("<sex_code>");
str.append(referrals.getSEX_CODE());
str.append("</sex_code>");
str.append("<sex_name>");
str.append(referrals.getSEX_NAME());
str.append("</sex_name>");
str.append("<age>");
str.append("</age>");
//调用switchset获取后半段
str2 = switchset.assembleXmlAfter();
str.append(str2);
System.out.println(str.toString());
return str.toString();
}
}
c3p0.config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
<property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="user">scott</property>
<property name="password">tiger</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">50</property>
</default-config>
</c3p0-config>