Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。
依赖的包: commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.1.jar
jaxb-impl-2.1.6.jar
jaxws-api-2.1.jar
jetty-6.1.5.jar
jetty-util-6.1.5.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar
spring jar 包, 用来支持xml配置:
aopalliance-1.0.jar
spring-core-2.0.4.jar
spring-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-web-2.0.4.jar
CXF jar包:
cxf-2.1.jar
以上jar 包 可从apache官方网站下载 apache-cxf-2.1.2.zip, 然后从apache-cxf-2.1.2/lib 目录中获得
1、首先在web.xml中配置
<!-- CXF -->
<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>/services/*</url-pattern>
</servlet-mapping>
<!-- CXF end -->
2、创建一个spring-module.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:http-conf = "http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-2.0.4.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<http-conf:conduit name="*.http-conduit">
<http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/>
</http-conf:conduit>
<!-- 系统集成计划反馈信息 -->
<jaxws:endpoint id="planFeedBackWsService" address="/planFeedBackWsService"
implementorClass="avicit.bt.webservice.service.PlanFeedBackWsService">
<jaxws:implementor>
<bean id="planFeedBackWsServiceImpl"
class="avicit.bt.webservice.service.impl.PlanFeedBackWsServiceImpl">
</bean>
</jaxws:implementor>
</jaxws:endpoint>
</beans>
@WebService(targetNamespace = "http://planFeedBackWsService.webservice.bt.avicit/")
public interface PlanFeedBackWsService{
/**
* 从系统进行对进展监控信息的更新
* @param newPassword 新密码
* @param unitCode 集团编码
* @return 成功:succeed;失败:error:错误信息。
*/
public String receiveFeedBack(String feedBackPlan);
/**
*
* @param feedBackPlan
* @return
* 在与别的项目集成中发现不能用@WebParam这个注解,否则会报这里要强调,service端不可以用webservice的相关注解,如WebMethod,Webresult,Webparam等注解。否则会出现unexpected element (uri:"", local:"args0").错误
*/
public String receiveDeliver(String feedBackDeliver);
}
@WebService
public class PlanFeedBackWsServiceImpl extends ServiceBase implements PlanFeedBackWsService {
@Autowired(required = true)
private SysUserProcessI sysUserService;
@Autowired(required = true)
private SysUserProcessI sysUserProcessI;
@Autowired(required = true)
private BtPmTaskInfoService pmTaskInfoService;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Override
public void delete(BeanBase arg0) throws DaoException {
}
@Override
public void insert(BeanBase arg0) throws DaoException {
}
@Override
public void update(BeanBase arg0) throws DaoException {
}
@Override
public String receiveFeedBack(String feedBackPlan) {
String returnString = "";
if (null == feedBackPlan || "".equals(feedBackPlan)) {
returnString = "反馈信息为空!";
} else {
// 解析json字符串
JSONObject jsonObject = JSONObject.fromObject(feedBackPlan);
// 得到计划的id
String ID = (String) jsonObject.get("ID");
BigDecimal ACTUAL_PROGRESS = null;
BigDecimal ACTUAL_WORKHOURS = null;
if (null != ID || !"".equals(ID)) {
try {
// 取出反馈后的信息并进行更新
// hibernateDao.update(sysUser);
BtPmTaskInfo taskInfo = pmTaskInfoService.getPmTaskInfoById(ID);
if (null != taskInfo) {
if (taskInfo.getPlanNature() != null && !"".equals(taskInfo.getPlanNature())) {
if ("6".equals(taskInfo.getPlanNature())) {
// 进展状态
if (jsonObject.get("ACTUAL_PROGRESS") != null && !"".equals(jsonObject.get("ACTUAL_PROGRESS"))) {
ACTUAL_PROGRESS = BigDecimal.valueOf(Double.parseDouble(jsonObject.get("ACTUAL_PROGRESS").toString()));
}
taskInfo.setActualProgress(ACTUAL_PROGRESS);
// 实际开始时间
if (jsonObject.get("ACTUAL_S_DATE") != null && !"".equals(jsonObject.get("ACTUAL_S_DATE"))
&& !"null".equals(jsonObject.get("ACTUAL_S_DATE"))) {
SimpleDateFormat sfEnd = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sfStart = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", java.util.Locale.ENGLISH);
Date ACTUAL_S_DATE = null;
String aceDate = sfEnd.format(sfStart.parse(jsonObject.get("ACTUAL_S_DATE").toString()));
ACTUAL_S_DATE = sdf.parse(aceDate);
taskInfo.setActualSDate(ACTUAL_S_DATE);
}
// 实际完成时间
if (jsonObject.get("ACTUAL_E_DATE") != null && !"".equals(jsonObject.get("ACTUAL_E_DATE"))
&& !"null".equals(jsonObject.get("ACTUAL_E_DATE"))) {
// 过来的结束日期与开始日期格式不一致所以要进行转换
SimpleDateFormat sfEnd = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sfStart = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", java.util.Locale.ENGLISH);
Date ACTUAL_E_DATE = null;
String aceDate = sfEnd.format(sfStart.parse(jsonObject.get("ACTUAL_E_DATE").toString()));
ACTUAL_E_DATE = sdf.parse(aceDate);
taskInfo.setActualEDate(ACTUAL_E_DATE);
}
// 实际工时
if (jsonObject.get("ACTUAL_WORKHOURS") != null && !"".equals(jsonObject.get("ACTUAL_WORKHOURS"))) {
ACTUAL_WORKHOURS = BigDecimal.valueOf(Double.parseDouble(jsonObject.get("ACTUAL_WORKHOURS").toString()));
taskInfo.setActualWorkhours(ACTUAL_WORKHOURS);
}
// 改变信息并进行更新
hibernateDao.update(taskInfo);
returnString = "success";
}
}
} else {
returnString = "在MPM系统中不存在ID为【" + ID + "】的计划!";
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return returnString;
}
@Override
public String receiveDeliver(String feedBackDeliver) {
String returnString = "";
if (null == feedBackDeliver || "".equals(feedBackDeliver)) {
returnString = "交付物信息为空!";
} else {
// 解析json字符串
JSONObject jsonObject = JSONObject.fromObject(feedBackDeliver);
// 得到交付物的id
String ID = (String) jsonObject.get("BILL_ID");
// 得到计划的idTASK_ID
String TASK_ID = (String) jsonObject.get("TASK_ID");
String url = "http://198.168.100.2:80/sysware/download/";
if (null != ID || !"".equals(ID)) {
try {
// 取出反馈后的信息并进行更新
BtPmTaskDeliver btPmTaskDeliver = pmTaskInfoService.getPmTaskDeliverById(ID);
BtPmTaskInfo taskInfo = pmTaskInfoService.getPmTaskInfoById(TASK_ID);
if (btPmTaskDeliver != null) {
// 交付物名称
if (jsonObject.get("BILL_NAME") != null && !"".equals(jsonObject.get("BILL_NAME"))) {
btPmTaskDeliver.setPdmurl(jsonObject.get("BILL_NAME").toString());
}
if (jsonObject.get("BILL_STATE") != null && !"".equals(jsonObject.get("BILL_STATE"))) {
String BILL_STATE = jsonObject.get("BILL_STATE").toString();
if ("0".equals(BILL_STATE)) {
BILL_STATE = "正常";
} else if ("1".equals(BILL_STATE)) {
BILL_STATE = "锁定";
}
btPmTaskDeliver.setDeliverableState(BILL_STATE);
}
if (jsonObject.get("PDMURL") != null && !"".equals(jsonObject.get("PDMURL"))) {
// String
// adapterUrl=url+"?ID="+jsonObject.get("PDMURL").toString()+"&&DIS=false&___ifr=true";
btPmTaskDeliver.setPdmurl(jsonObject.get("PDMURL").toString());
}
// 修改人
if (jsonObject.get("UPDATER") != null && !"".equals(jsonObject.get("UPDATER"))) {
btPmTaskDeliver.setLastUpdatedBy(jsonObject.get("UPDATER").toString());
}
// 更新计划实际开始时间
btPmTaskDeliver.setDeliverActualStartDate(taskInfo.getActualSDate());
btPmTaskDeliver.setLastUpdateDate(new Date());
// 改变信息并进行更新交付物
hibernateDao.update(btPmTaskDeliver);
returnString = "更新交付物成功";
} else {
// 执行新增
String BILL_ID = jsonObject.get("BILL_ID").toString();
if (null == taskInfo) {
returnString = "新增交付物的计划ID=【" + ID + "】在MPM不存在";
} else {
// 执行新增
String BILL_NAME = jsonObject.get("BILL_NAME").toString();
String CREATOR = jsonObject.get("CREATOR").toString();
String BILL_STATE = jsonObject.get("BILL_STATE").toString();
String PDMURL = jsonObject.get("PDMURL").toString();
String adapterUrl = "";
BtPmTaskDeliver deliver = new BtPmTaskDeliver();
if (BILL_ID != null && !"".equals(BILL_ID)) {
deliver.setId(BILL_ID);
}
if (null != BILL_NAME && !"".equals(BILL_NAME)) {
deliver.setDeliverableName(BILL_NAME);
}
if (PDMURL != null && !"".equals(PDMURL)) {
// adapterUrl=url+"?ID="+PDMURL+"&&DIS=false&___ifr=true";
deliver.setPdmurl(PDMURL);
}
if (null != CREATOR && !"".equals(CREATOR)) {
deliver.setCreatedBy(CREATOR);
}
if (null != BILL_STATE && !"".equals(BILL_STATE)) {
if ("0".equals(BILL_STATE)) {
BILL_STATE = "正常";
} else if ("1".equals(BILL_STATE)) {
BILL_STATE = "锁定";
}
deliver.setDeliverableState(BILL_STATE);
}
if (null != TASK_ID && !"".equals(TASK_ID)) {
deliver.setTaskId(TASK_ID);
}
// 默认文件类型
deliver.setDeliverType("WJ");
// 设置实际开始时间
deliver.setDeliverActualStartDate(taskInfo.getActualSDate());
// 设置计划实际完成时间
deliver.setDeliverAcutalEndDate(new Date());
PojoUtil.setSysProperties(deliver, OpType.insert);
this.hibernateDao.save(deliver);
hibernateDao.flush();
SysLogUtil.log(deliver, null, OpType.insert, OpResult.success);
// hibernateDao.save(deliver);
// 保存数据
returnString = "新增交付物成功!";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return returnString;
}
public static void main(String[] args) throws Exception {
//方法一
/* JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
String address = "http://127.0.0.1:8080/MPM/services";
factory.setAddress(address+"/changePasswordWsService");
factory.setServiceClass(ChangePasswordWsService.class);
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
ChangePasswordWsService service = (ChangePasswordWsService) factory.create();
try{
String xml = service.changePassword("D24D5274BC5787A8CE551BFD7C5CE784", "28101129");
System.out.println(xml);
}catch(Exception e){
e.printStackTrace();
}*/
// 方法二动态调用
String wsdlUrl = "http://127.0.0.1:8080/MPM/services/planFeedBackWsService?wsdl";
String wsNameSpace = "http://planFeedBackWsService.webservice.bt.avicit/";
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(wsdlUrl);
String json="{'ID':'111'}" ;
QName name = new QName(wsNameSpace,"receiveFeedBack");
Object[] xml = null;
try {
//xml = client.invoke(name, "D24D5274BC5787A8CE551BFD7C5CE784", "28101129");
//xml = client.invoke(name, new Object[]{"A","b"});
xml = client.invoke(name, json);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("xml="+xml[0]);