1介绍
CXF号称是SOA框架,上一篇已经简单介绍了下WebService和CXF,这篇不再累述.
可能下面这张图可以很清楚地说明WebService在J2EE系统间的作用,比如系统间信息交互,或者新旧系统的整合等等.
2开发WebService服务端(提供服务的系统)
2.1新建项目
新建一个WEB项目,并且导入Spring和CXF至少所需要的JAR包,下面是这个项目的JAR,不过最好用maven管理这些依赖.
2.2搭建Spring+CXF环境
编写web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SpringCXF_Server</display-name>
<!-- Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置CXF请求Servlet -->
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 这里是拦截所有请求 -->
<servlet-mapping>
<servlet-name>CXF</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
在某个目录下新建一个spring容器配置文件:这里命名为applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<!-- apache-cxf3.0以上版本不在需要该xml
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> -->
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 服务提供组件 -->
<bean id="userServiceImpl" class="com.lin.ws.impl.UserServiceImpl"></bean>
<!-- implementor参数默认是一个bean,为了使该参数依赖其他bean,在注入的时候写上"#id"
address是该服务的地址,默认是项目的地址+这里定义的参数,比如:http://locahost:8080/SpringCXF_Server/UserService-->
<jaxws:endpoint id="userService" implementor="#userServiceImpl"
address="/UserService" />
</beans>
2.3设计服务接口和实体类
package com.lin.ws.entity;
public class User {
private Integer id;
private String name;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(Integer id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ "]";
}
}
package com.lin.ws.intf;
import javax.jws.WebService;
import com.lin.ws.entity.User;
@WebService
public interface IUserService {
public User login(String name,String password);
}
2.4编写接口实现类
package com.lin.ws.impl;
import javax.jws.WebService;
import com.lin.ws.entity.User;
import com.lin.ws.intf.IUserService;
//serviceName定义该服务的名称,将显示在wsdl文档中
@WebService(endpointInterface="com.lin.ws.intf.IUserService",serviceName="UserService")
public class UserServiceImpl implements IUserService {
@Override
public User login(String name, String password) {
return new User(1, name, password);
}
}
2.5发布服务
在tomcat里运行这个web项目就发布了服务,访问wsdl文档:http://locahost:8080/SpringCXF_Server/UserService?wsdl
系统的目录结构
3如何访问该服务
3.1开发客户端系统(该系统访问WebService服务)
新建一个web项目(例如该项目是基于Spring+Struts2的),导包,由于需要Jaxws的接口代理,也导入CXF的jar包
并写好各个xml文件....
3.2生成客户端代码
进入该项目的src目录,用wsdl2java生成java代码
3.3编写一个Struts2 action处理请求
package com.lin.ws.action;
import com.lin.ws.intf.IUserService;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private IUserService userService;//服务端接口
private String name;
private String password;
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception
{
System.out.println("Login Success"+userService.login(name, password).toString());
return SUCCESS;
}
}
在Spring容器里配置这个action,并且注入它依赖的接口:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<bean name="loginAction" class="com.lin.ws.action.LoginAction">
<property name="userService" ref="userService"></property>
</bean>
<!-- 配置webservice服务端接口代理,声明的id可用于依赖注入
address的参数是该服务的地址 -->
<jaxws:client id="userService"
serviceClass="com.lin.ws.intf.IUserService"
address="http://localhost:8080/SpringCXF_Server/UserService">
</jaxws:client>
</beans>
在struts里面配置这个action:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="loginAction">
<result>/WEB-INF/content/success.jsp</result>
</action>
</package>
</struts>
然后运行,发送请求访问这个action......
系统的目录结构
4总结
一个基于Spring的J2EE系统发布了某些WebService服务,那么该系统就是服务的提供者,它对外提供一份WSDl文档用于它的客户访问它所提供的服务.
其次,客户在得到该WSDL文档后,在自己原有的系统或开发新系统来生成服务端接口代理,客户调用这些接口就像调用本地接口一样,从而实现多系统间信息交流或多系统整合.系统的