如何在Spring 3 MVC框架下结合CXF开发Webservice服务
1:在Web.xml中配置WebService URL过滤器。
<servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
2:定义WebService接口
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(serviceName="idsWebService", targetNamespace="http://idsws.ids.founder.com/")
public interface IIDSWebService {
/**
* 获取科室
* @return
*/
public WDepartmentArr getDepartments();
/**
* 取消预约挂号
* @param patientId IDS患者标识
* @param idsRegId IDS预约标识
* @return
*/
public String cancelRegister(@WebParam(name = "patient_id") String patientId, @WebParam(name = "ids_reg_id") String idsRegId);
/**
* 创建患者
* @param patient
* @return IDS患者标识
*/
public String createPatient(WPatient patient);
}
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "WPatient")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "WPatient", propOrder = { "patientId","name", "sex", "birthday", "certNo",
"certType", "homeStreet", "homeTel","cellPhone", "relationName", "relationTel",
"healthCardNo", "fromCommunity"})
public class WPatient {
/**
* 证件类型字典: 01:身份证 03:护照 99:其他证件
*
* 性别字典: 1:男 2女 9:未说明的性别
*/
@XmlElement(name = "patient_id")
private String patientId; // IDS患者标识 在给西城区提供的接口中 所有的patientId在IDS中都当做patient表中的patient_id
@XmlElement(required = true)
private String name; // 姓名
@XmlElement(required = true)
private String sex;// 性别代码
@XmlElement(required = true)
private String birthday;
@XmlElement(name = "cert_no", required = true)
private String certNo; // 证件号
@XmlElement(name = "cert_type", required = true)
private String certType; // 证件类别
@XmlElement(name = "home_street")
private String homeStreet;// 患者的家庭住址(可选)
@XmlElement(name = "home_tel")
private String homeTel;// 患者的家庭住址(可选)
@XmlElement(name = "cell_phone")
private String cellPhone;// 患者手机号
@XmlElement(name = "relation_name")
private String relationName;// 患者的家属姓名(可选)
@XmlElement(name = "relation_tel")
private String relationTel;// 患者的家属电话(可选)
@XmlElement(name = "health_card_no")
private String healthCardNo;// 医保号(可选)
@XmlElement(name = "from_community", required=true)
private String fromCommunity;// 来源机构编码(IDS提供)
/*
@XmlElement(name = "his_id")
private String hisId; // his患者标识
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
... ....
}
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "WDepartmentArr", propOrder = { "wDepartment" })
public class WDepartmentArr {
@XmlElement(name = "wDepartment")
private List<WDepartment> wDepartment;
public List<WDepartment> getwDepartment() {
if (null == wDepartment) {
wDepartment = new ArrayList<WDepartment>();
}
return wDepartment;
}
public void setwDepartment(List<WDepartment> wDepartment) {
this.wDepartment = wDepartment;
}
}
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import com.founder.ids.entity.Clinic;
@XmlRootElement(name="WDepartment")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={
"detpSn",
"name",
"abbCode",
"abbName"})
public class WDepartment {
@XmlElement(name = "detp_sn", required = true)
private String detpSn;
@XmlElement(required = true)
private String name;
@XmlElement(name = "abb_code", required = true)
private String abbCode;
@XmlElement(name = "abb_name", required = true)
private String abbName;
public WDepartment(Clinic clinic){
this.detpSn = clinic.getCode();
this.name = clinic.getName();
this.abbCode = clinic.getAbbCode();
this.abbName = clinic.getAbbName();
}
public WDepartment() {
}
public String getDetpSn() {
return detpSn;
}
......
}
3. 完成接口实现
@Service("idsWebServiceImpl")@WebService(targetNamespace="http://idsws.ids.founder.com/" )
public class IDSWebservice implements IIDSWebService {
private static Logger logger = Logger.getLogger(IDSWebservice.class);
......
}
4. 在spring配置文件applicationContext-web.xml配置发布Webservice
<jaxws:endpoint id="idsWebService" implementor="#idsWebServiceImpl" address="/index" publish="true" > </jaxws:endpoint>
5. 在tomcat部署应用访问地址如下:
* 使用CXF命令将定义好的WebService接口class生成WSDL文件,具体代码如下:
/**
* @description 使用CXF工具将Java类生成WSDL
* @author zhu_qhua
*/
public class Java2WSDL {
private Class<?> className;
public String[] args1,args2,args3;
/**
* 构造函数
* @param args 要生成WSDL的Java类
*/
public Java2WSDL(Class<?> className) {
this.className = className;
//根据Hello.class生成Hello.wsdl,生成的wsdl文件放在resource目录下
args1=new String[]{"-wsdl","-d","./resource",this.className.getName()};
//根据Hello.class生成Hello.wsdl,生成的文件放在根目录下的.example下
args2=new String[]{"-wsdl","-cp", "./example",this.className.getName()};
// 根据Hello.class生成wsdl文件,文件命名为myHello.wsdl,放在根目录下
args3=new String[]{"-o","myHello.wsdl","-wsdl",this.className.getName()};
}
/**
* 构造函数
* @param className 要生成WSDL的Java类
* @param wsdlFileName 要生成WSDL的文件名称(不包含扩展名)
*/
public Java2WSDL(Class<?> className,String wsdlFileName) {
this.className = className;
//根据Hello.class生成Hello.wsdl,生成的wsdl文件放在resource目录下
args1=new String[]{"-wsdl","-d","./resource",this.className.getName()};
//根据Hello.class生成Hello.wsdl,生成的文件放在根目录下的.example下
args2=new String[]{"-wsdl","-cp", "./example",this.className.getName()};
// 根据Hello.class生成wsdl文件,文件命名为myHello.wsdl,放在根目录下
args3=new String[]{"-o",wsdlFileName+".wsdl","-wsdl",this.className.getName()};
}
/*public void java2WSDL(String[] args){
JavaToWS javaToWS = new JavaToWS(args);
try {
javaToWS.run();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Java2WSDL java2WSDL = new Java2WSDL(IDSWebservice.class);
java2WSDL.java2WSDL(java2WSDL.args1);
}*/
}