我们项目的整个架构使用的比较流行的WSH MVC组合,即webwork2 + Spring + Hibernate;
1.首先集成Apacha CXF WebService 到 Spring 框架中;
apache cxf 下载地址:
http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip 在spring context配置文件中引入以下cxf配置
<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.xml中添加过滤器:
Xml代码
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
2.开发服务端WebService接口:
Java代码
-
-
-
-
-
-
- @WebService
- public interface WebServiceSample {
-
-
-
-
-
-
-
- String say(String hello);
-
-
-
-
-
-
- String sayUserName(
- @WebParam(name = "user")
- UserDTO user);
-
-
-
-
-
- public
- @WebResult(partName="o")
- ListObject findUsers();
-
- }
由简单到复杂定义了三个接口,模拟业务需求;
3.实现接口
-
-
-
-
-
- @WebService(endpointInterface = "cn.org.coral.biz.examples.webservice.WebServiceSample")
- public class WebServiceSampleImpl implements WebServiceSample {
-
- public String sayUserName(UserDTO user) {
- return "hello "+user.getName();
- }
-
- public String say(String hello) {
- return "hello "+hello;
- }
-
- public ListObject findUsers() {
- ArrayList<Object> list = new ArrayList<Object>();
-
- list.add(instancUser(1,"lib"));
- list.add(instancUser(2,"mld"));
- list.add(instancUser(3,"lq"));
- list.add(instancUser(4,"gj"));
- ListObject o = new ListObject();
- o.setList(list);
- return o;
- }
-
- private UserDTO instancUser(Integer id,String name){
- UserDTO user = new UserDTO();
- user.setId(id);
- user.setName(name);
- return user;
- }
- }
4.依赖的两个类:用户对象与List对象
Java代码
-
-
-
-
-
-
-
- @XmlAccessorType(XmlAccessType.FIELD)
- @XmlType(name = "User")
- public class UserDTO {
-
- protected Integer id;
-
- protected String name;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer value) {
- id = value;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String value) {
- name = value;
- }
- }
关于List对象,参照了有关JWS的一个问题中的描述:DK6.0 自带的WebService中 WebMethod的参数好像不能是ArrayList 或者其他List
传递List需要将List 包装在其他对象内部才行 (个人理解 如有不对请指出) ,我在实践中也遇到了此类问题.通过以下封装的对象即可以传递List对象.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- @XmlAccessorType(XmlAccessType.FIELD)
- @XmlType(name = "listObject", propOrder = { "list" })
- public class ListObject {
-
- @XmlElement(nillable = true)
- protected List<Object> list;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public List<Object> getList() {
- if (list == null) {
- list = new ArrayList<Object>();
- }
- return this.list;
- }
-
- public void setList(ArrayList<Object> list) {
- this.list = list;
- }
-
- }
5.WebService 服务端 spring 配置文件 ws-context.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
- default-autowire="byName" default-lazy-init="true">
-
- <jaxws:endpoint id="webServiceSample"
- address="/WebServiceSample" implementor="cn.org.coral.biz.examples.webservice.WebServiceSampleImpl"/>
-
- </beans>
WebService 客户端 spring 配置文件 wsclient-context.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
- default-autowire="byName" default-lazy-init="true">
-
-
- <bean id="identityValidateServiceClient" class="cn.org.coral.admin.service.IdentityValidateService"
- factory-bean="identityValidateServiceClientFactory" factory-method="create" />
-
- <bean id="identityValidateServiceClientFactory"
- class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
- <property name="serviceClass"
- value="cn.org.coral.admin.service.IdentityValidateService" />
- <property name="address"
- value="http://88.148.29.54:8080/coral/services/IdentityValidateService"/>
- </bean>
-
- </beans>
6.发布到tomcat服务器以后通过以下地址即可查看自定义的webservice接口生成的wsdl:
http://88.148.29.54:8080/aio/services/WebServiceSample?wsdl
7.调用WebService接口的Junit单元测试程序
- package test.coral.sample;
-
- import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
-
- import cn.org.coral.biz.examples.webservice.WebServiceSample;
- import cn.org.coral.biz.examples.webservice.dto.UserDTO;
-
- public class TestWebServiceSample extends
- AbstractDependencyInjectionSpringContextTests {
- WebServiceSample webServiceSampleClient;
-
- public void setWebServiceSampleClient(WebServiceSample webServiceSampleClient) {
- this.webServiceSampleClient = webServiceSampleClient;
- }
-
- @Override
- protected String[] getConfigLocations() {
- setAutowireMode(AUTOWIRE_BY_NAME);
-
- return new String[] { "classpath:/cn/org/coral/biz/examples/webservice/wsclient-context.xml" };
- }
-
- public void testWSClinet(){
- Assert.hasText(webServiceSampleClient.say(" world"));
- }
- }