写在前面的话@H_502_5@
一般在项目中,如果使用Spring,我们一般会将CXF与Spring集成,当然我实际的项目中我们也是这么做的,这次笔记记录一下CXF与Spring的集成发布一个服务。
服务端@H_502_5@
服务端工程结构如下:
首先创建一个动态工程,按图创建相应的目录,同时将CXF的jar包放置于WebContent/WEB-INF/lib下面,接下来我拉编写服务接口和实现类
服务接口
package com.wds.ws.server.spring; import javax.jws.WebService; /** * 服务接口 * @author wds * */ @WebService public interface HelloService { public String sayHi(String userName); }
服务实现类
package com.wds.ws.server.spring.impl; import java.util.Date; import javax.jws.WebService; import com.wds.ws.server.spring.HelloService; /** * 服务实现类 * 关于@WebService在上面有介绍过 * @author wds * */ @WebService(endpointInterface="com.wds.ws.server.spring.HelloService",portName="hwPort",serviceName="hwService") public class HelloServiceImpl implements HelloService { @Override public String sayHi(String userName) { String result = new Date() + " " + userName; System.out.println("Server: " + result); return result; } }
服务端配置
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>webAppRootKey</param-name> <param-value>cxf.root</param-value> </context-param> <!-- UTF-8编码配置 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 指定配置文件的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml </param-value> </context-param> <!-- 配置Spring的监听 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置CXFServlet --> <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形式都转向WebService,两种URL形式可以访问服务 --> <url-pattern>/soa-infra/services/default/*</url-pattern> <url-pattern>/cxf/*</url-pattern> </servlet-mapping> </web-app>
web.xml中重置的两个配置一个spring的监听,这个不多说,另一个是关于CXFServlet的配置。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 除添加Spring的schmea之外,还要添加关于cxf的schema,其前缀可以随意命名,我使用cxf,也可以使用jaxws --> <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:cxf="http://cxf.apache.org/jaxws" 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"> <!-- 指定到类路径下面的meta-inf/cxf中去寻找cxf.xml和cxf-servlet.xml这两个文件 --> <import resource="classpath:meta-inf/cxf/cxf.xml" /> <import resource="classpath:meta-inf/cxf/cxf-servlet.xml" /> <!-- 提供服务的bean --> <bean id="helloWorldWs" class="com.wds.ws.server.spring.impl.HelloServiceImpl" /> <!-- implementor:指定服务的提供者,有两种方式:1是类名,2是bean的名字 address:服务路径 --> <cxf:endpoint implementor="#helloWorldWs" address="/helloworld" > </cxf:endpoint> </beans>至此,服务端开发结束,部署,运行服务器,输入 http://localhost:8080/com.wds.ws.server.spring/cxf/helloworld?wsdl,即可访问或者xxx/soa-infra/services/default/helloworld?wsdl也可以。
客户端@H_502_5@
使用cxf的wsdl2java命令,wsdl2java -p com.wds.ws.client.spring http://localhost:8080/com.wds.ws.server.spring/cxf/helloworld?wsdl,生成客户端代码,将这些代码复制到客户端工程中,工程结构如下。

在前面的几节笔记中,我们使用过cxf开发客户端,但是他们无法通过修改服务的ip地址来访问服务,有时候我们在开发服务的客户端时,不知道服务端部署的IP地址,因此我们可以使用另外一种方式来开发客户端,代码如下:
package com.wds.ws.client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.wds.ws.client.spring.HelloService;
/**
* 客户端
* @author wds
*
*/
public class Client {
/**
* 命令
* 我的cxf的包放到f:/package,可根据实际目录调整
* F:\Package\apache-cxf-2.7.3\bin>wsdl2java -p com.wds.ws.client.spring http://localhost:8080/com.wds.ws.server.spring/cxf/helloworld?wsdl
*/
/**
* WSDL的地址
*/
private final static String url = "http://localhost:8080/com.wds.ws.server.spring/cxf/helloworld?wsdl";
/**
* 命名空间,在WSDL根节点中的targetNameSpace
*/
private final static String nameSpace = "http://impl.spring.server.ws.wds.com/";
/**
* 服务名称,在WSDL文件中的节点为<wsdl:sevice name="XXX">的name属性值
*/
private final static String sName = "hwService";
/**
* @param args
*/
public static void main(String[] args) {
try {
//生成wsdlUrl对象
URL wsdlUrl = new URL(url);
//ServiceName
QName serviceName = new QName(nameSpace,sName);
//创建Service对象
Service service = Service.create(wsdlUrl,serviceName);
//获取port,HelloService为port的类型
HelloService hwService = service.getPort(HelloService.class);
//执行方法调用
String result = hwService.sayHi("wds");
//打印结果
System.out.println(result);
} catch (MalformedURLException e) {
e.printstacktrace();
}
}
}
观察客户端的代码,与我们前几次笔记中的客户端是有区别的,通过这上述的方式,可实现服务端地址的可配置。
小结@H_502_5@
package com.wds.ws.client; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import com.wds.ws.client.spring.HelloService; /** * 客户端 * @author wds * */ public class Client { /** * 命令 * 我的cxf的包放到f:/package,可根据实际目录调整 * F:\Package\apache-cxf-2.7.3\bin>wsdl2java -p com.wds.ws.client.spring http://localhost:8080/com.wds.ws.server.spring/cxf/helloworld?wsdl */ /** * WSDL的地址 */ private final static String url = "http://localhost:8080/com.wds.ws.server.spring/cxf/helloworld?wsdl"; /** * 命名空间,在WSDL根节点中的targetNameSpace */ private final static String nameSpace = "http://impl.spring.server.ws.wds.com/"; /** * 服务名称,在WSDL文件中的节点为<wsdl:sevice name="XXX">的name属性值 */ private final static String sName = "hwService"; /** * @param args */ public static void main(String[] args) { try { //生成wsdlUrl对象 URL wsdlUrl = new URL(url); //ServiceName QName serviceName = new QName(nameSpace,sName); //创建Service对象 Service service = Service.create(wsdlUrl,serviceName); //获取port,HelloService为port的类型 HelloService hwService = service.getPort(HelloService.class); //执行方法调用 String result = hwService.sayHi("wds"); //打印结果 System.out.println(result); } catch (MalformedURLException e) { e.printstacktrace(); } } }观察客户端的代码,与我们前几次笔记中的客户端是有区别的,通过这上述的方式,可实现服务端地址的可配置。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。