您的位置:首页 > 编程语言 > Java开发

spring中发布基于cxf的webservice

2014-08-13 16:48 411 查看
以自己写的回写预约记录状态服务为例

服务器端:

1.编写web服务接口:

IRegRecordService.java


package com.jlit.hrs.webservice;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.jlit.uums.webservice.bean.UserMenus;
/**
* 预约记录service
* @author xuht
* @time 2014-5-28 下午3:26:15
* @file IRegRecordService.java
*/
@WebService
public interface IRegRecordService {
/**更新订单状态
* @param orderId 订单id
* @param status  更改的状态
* @return  void
*/

public void updateRecordStatus(@WebParam(name="orderId") String orderId,@WebParam(name="status")String status);
/**
* 根据订单流水号判断是否该订单有效
* @param orderNo 订单流水号
* @return 返回是该订单是否有效的字符串:"valid"有效,"invalid"无效
*/
public String isValid(@WebParam(name="orderNo")String orderNo);
}
2.编写服务接口实现类:

package com.jlit.hrs.webservice.impl;

import org.apache.log4j.Logger;

import com.jlit.hrs.service.RegRecordService;
import com.jlit.hrs.webservice.IRegRecordService;
public class IRegRecordServiceImpl implements IRegRecordService {
private final static Logger logger=Logger.getLogger(IRegRecordServiceImpl.class);
private RegRecordService rdService;
public RegRecordService getRdService() {
return rdService;
}
public void setRdService(RegRecordService rdService) {
this.rdService = rdService;
}
@Override
public void updateRecordStatus(String orderId, String status) {
// TODO Auto-generated method stub
rdService.updateRecordStatus(orderId, status);
}
@Override
public String isValid(String orderNo) {
return rdService.isValid(orderNo);
}

}
3.在web。xml中配置cxf 前端servlet

<!-- CXF Servlet 的定义,以及它的映射 -->

<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>

<!-- CXF Servlet 的定义,以及它的映射end -->

4.在spring 配置发布WEB服务接口:applicationcontext-csf-server.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:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Import apache CXF bean definition -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<!-- 预约 web service -->
<bean id="ws_regRecordService" class="com.jlit.hrs.webservice.impl.IRegRecordServiceImpl">
<property name="rdService" ref="regRecordService"></property>
</bean>
<jaxws:endpoint id="ws_serService_rd" implementor="#ws_regRecordService" address="/regRecordWebService" />

</beans>


ok 一个服务器端配置完毕。

测试:在浏览器中输入:



客户端配置:

1.web接口拷到对应项目里:

spring中配置该接口:applicationcontext-csf-client.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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<!-- CXF webservices 客户端配置 -->
<!-- 预约service -->
<jaxws:client id="regRecordWebService" serviceClass="com.jlit.hrs.webservice.IRegRecordService"
address="http://127.0.0.1:8090/hrs/ws/regRecordWebService">
</jaxws:client>
</beans>
java测试:WebServiceTest.java

package com.jlit.hrs.ws.test;

import org.junit.Before;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jlit.mms.webservice.IOrdersService;

import com.jlit.model.Orders;

public class WebServiceTest {

private IOrdersService iOrdersService;

@Before

public void init(){

ApplicationContext ctx=new ClassPathXmlApplicationContext("com/jlit/hrs/resources/applicationContext-cxf-client.xml");

iOrdersService=(IOrdersService) ctx.getBean("iOrdersService");

}

@Test

public void test(){

Orders orders=iOrdersService.createRegRecourdOrder("1111", "lisanxiang", 20.56, "挂号预约订单");

System.out.println("订单号:"+orders.getOrderNo());

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: