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

WebService-04-CXF与Spring集成开发WebService服务

2013-06-24 09:28 591 查看

写在前面的话

一般在项目中,如果使用Spring,我们一般会将CXF与Spring集成,当然我实际的项目中我们也是这么做的,这次笔记记录一下CXF与Spring的集成发布一个服务。

服务端

服务端工程结构如下:



首先创建一个动态工程,按图创建相应的目录,同时将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也可以。

客户端

使用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();
}
}

}
观察客户端的代码,与我们前几次笔记中的客户端是有区别的,通过这上述的方式,可实现服务端地址的可配置。

小结

CXF与Spring的集成,更多的在于配置文件,到此,我们已经使用过两种方式的客户端,一类是通过wsimport、cxf的wsdl2java生成客户端,然后通过new Serivce的方式创建服务,一类是现在的这种方式,通过配置服务的wsdl地址、命名、服务名称,实现服务端的动态可配置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: