CXF发布webservice

本文介绍使用Apache CXF发布WebService的过程,包括服务端配置、发布及客户端调用示例。示例展示了如何通过Jetty发布服务,并通过Spring配置客户端进行调用。
[size=medium][size=large][size=medium]服务端配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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" />

<bean id="address" class="cn.com.pop.service.AddressManager"/>

<bean id="inInter" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>

<bean id="outInter" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>

<bean id="myPasswordCallback" class="cn.com.pop.interceptor.ServerPasswordCallback"/>

<bean id="checkSignInter" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken"/>
<entry key="passwordType" value="PasswordDigest"/>
<entry key="signaturePropFile" value="..."/>
<entry key="passwordCallbackRef">
<ref bean="myPasswordCallback"/>
</entry>
</map>
</constructor-arg>
</bean>

<jaxws:server id="addressServer" serviceClass="cn.com.pop.service.AddressManager" address="/Address">
<jaxws:serviceBean>
<ref bean="address"/>
</jaxws:serviceBean>
<jaxws:inInterceptors>
<ref bean="inInter"/>
<!-- <ref bean="checkSignInter"/> -->
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outInter"/>
</jaxws:outInterceptors>
</jaxws:server>

<jaxws:endpoint id="helloService" implementor="cn.com.pop.service.impl.HelloServiceImpl" address="/HelloService">
<jaxws:inInterceptors>
<ref bean="checkSignInter"/>
</jaxws:inInterceptors>
</jaxws:endpoint>

</beans>

服务类
package cn.com.pop.service.impl;

import javax.jws.WebService;

import cn.com.pop.service.HelloService;
import cn.com.pop.vo.Student;

@WebService(endpointInterface = "cn.com.pop.service.HelloService", serviceName = "helloService")
public class HelloServiceImpl implements HelloService {

public String sayHello(String str) {
return "Hello ".concat(str);
}

public Student getStudentBean() {
Student student = new Student();
student.setName("Jim Green");
student.setSex(1);
return student;
}

}

发布服务:
/**
* 利用CXF中的Jetty来发布自己的WebService
*
* 利用CXF来生成客户端需要的一些文件和WebService接口,命令如下:
* D:\Program Files\Java\apache-cxf-2.2.4\bin>wsdl2java -p cn.com.pop.client.jetty.clientbean -d D:\ http://localhost:9000/helloService?wsdl
*
* wsdl2java是CXF官方的帮助工具,可以下载一个官方的CXF包 本案例用的版本是apache-cxf-2.2.4 ,解压之后可以拷贝到自己的盘符,本案例拷贝到D:\Program Files\Java\目录
* apache-cxf-2.2.4目录下有个bin目录,该目录下有很多官方提供的工具,其中有个wsdl2java,可以用命令wsdl2java -h来查看命令参数,如果感兴趣可以自己找找帮助文件
* 命令:wsdl2java -p com.jd.sop1 -all -d D:\ http://localhost:9000/helloService?wsdl
* 如果WebService换了发布URL之后需要重新生成客户端文件,或者手动修改客户端文件中对应的URL
*
* 还可以用别的方法来生成客户端需要的文件
*
* @author Administrator
*
*/
public class JettyTestCase {

public static void main(String[] arg){
System.out.println("Starting Server");

HelloServiceImpl impl = new HelloServiceImpl();
String addr = "http://localhost:9000/helloService";

//通过CXF提供的Jetty方式发布WebService
Endpoint.publish(addr,impl);

System.out.println("WebServer Register Success");
}

}


客户端:
<?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: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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<jaxws:client id="helloService" serviceClass="cn.com.pop.client.web.clientbean.HelloService"
address="http://localhost:9000/helloService">
<jaxws:inInterceptors>
<ref bean="logIn"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="logOut"/>
<!-- <ref bean="saajOut"/> -->
<ref bean="wss4jOut"/>
</jaxws:outInterceptors>
</jaxws:client>

<bean id="logIn" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean id="logOut" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
<bean id="saajOut" class="org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor" />
<bean id="wss4jOut" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="user" value="joe" />
<entry key="passwordType" value="PasswordText" />
<entry key="passwordCallbackClass" value="cn.com.pop.client.web.util.ClientPasswordCallback" />
</map>
</constructor-arg>
</bean>

</beans>

package cn.com.pop.client.web;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.pop.client.web.clientbean.HelloService;

/**
* 利用spring配置来获取WebService
*
* @author
*
*/
public class SpringCase {

public static void main(String[] args) {
// 利用spring的上下文来加载我们需要的服务
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:bean-client.xml");
ctx.registerShutdownHook();
HelloService client = (HelloService) ctx.getBean("helloService");
String str = client.sayHello("中国,测试成功");
System.out.println("str=" + str);
// System.exit(0);
}
}


package cn.com.pop.client.jetty;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import cn.com.pop.client.jetty.clientbean.HelloService;
import cn.com.pop.client.jetty.clientbean.Student;

/**
*
* 利用JaxWsProxyFactoryBean来获取WebService
*
*/
public class TestCase {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

factory.setAddress("http://localhost:9000/helloService");

factory.setServiceClass(HelloService.class);
HelloService obj = (HelloService) factory.create();
System.out.println(obj.sayHello("中国,测试成功"));

Student student = obj.getStudentBean();
System.out.println(student.getName());
}

}

[/size][/size][/size]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值