首页 > 代码库 > webservice之cxf样例
webservice之cxf样例
1.首先服务接口。
package com.demo;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(@WebParam(name="text")String text);
String sayHiToUser(User user);
String[] SayHiToUserList(List<User> userList);
}
2.编写服务实现
package com.demo;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
@WebService(endpointInterface="com.demo.HelloWorld",serviceName="HelloWorld")
public class HelloWorldImpl implements HelloWorld {
Map<Integer, User> users = new LinkedHashMap<Integer, User>();
public String sayHi(String text) {
return "Hello " + text;
}
public String sayHiToUser(User user) {
users.put(users.size()+1, user);
return "Hello "+ user.getName();
}
public String[] SayHiToUserList(List<User> userList) {
String[] result = new String[userList.size()];
int i=0;
for(User u:userList){
result[i] = "Hello " + u.getName();
i++;
}
return result;
}
}
3.公布服务
方法一:使用Sun JAX-WS 2中Endpoint.publish进行公布。(不须要其它配置与
包)
Endpoint endpoint =
Endpoint.publish("http://localhost:8080/WSCXF/helloService", new HelloServiceImpl());//这里是实现类 System.out.println("WS公布成功!");
方法二:用CXF的JaxWsServerFactoryBean类进行公布。(须要CXF相关包)
HelloServiceImpl impl = new HelloServiceImpl();
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean(); factoryBean.setAddress("http://localhost:8080/WSCXF/helloService"); factoryBean.setServiceClass(IHelloService.class);//接口类 factoryBean.setServiceBean(impl); factoryBean.create();
System.out.println("WS公布成功!");
方法三:利用cxf和spring整合在tomcat下进行公布。
详细方法在后面的spring整合cxf时讲到。
方法四:重写loadBus方法。
书写一个类覆盖org.apache.cxf.transport.servlet.CXFNonSpringServlet的loadBus方法指定BUS以及公布你的web服务。
详细可查阅资料:
http://blog.csdn.net/zq9017197/archive/2010/12/26/6099684.aspx
4.查看web服务是否公布成功:
方法一二四訪问http://localhost:8080/WSCXF/helloService?wsdl 查看wsdl文件,方法三在后面的spring整合cxf时讲到。
二:client调用
方法一:使用标准的JAX-WS的API完毕client调用(不须要导入不论什么CXF包)
//注意。此处http://service.jaxb.com/ 来源于wsdl文件里targetNamespace
QName qName = new QName("http://service.jaxb.com/","HelloServiceImplService");
HelloServiceImplService helloServiceImplService = new HelloServiceImplService(
new URL("http://localhost:8080/WSCXF/helloService?wsdl"),qName);
IHelloService helloService =(IHelloService)helloServiceImplService.getPort(IHelloService.class);
方法二:使用CXF中JaxWsProxyFactoryBeanclient代理工厂调用web服务(须要导入CXF相关包)
JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
soapFactoryBean.setAddress("http:// localhost:8080/WSCXF/helloService");
soapFactoryBean.setServiceClass(IHelloService.class);
Object o = soapFactoryBean.create();
IHelloService helloService = (IHelloService)o;
方法三:
String endPointAddress = "http:// localhost:8080/WSCXF/helloService";
Service service = Service.create(
new QName("http://service.jaxb.com/","IHelloService"));
service.addPort(
new QName("http://service.jaxb.com/", "IHelloServicePort"), SOAPBinding.SOAP11HTTP_BINDING, endPointAddress);
IHelloService helloService = service.getPort(IHelloService.class);
方法四:(须要导入CXF相关包)
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient("http://127.0.0.1:8080/WSCXF/helloService?wsdl");
//sayHello 为接口中定义的方法名称 张三为传递的參数 返回一个Object数组
Object[] objects=client.invoke("sayHello", "张三");
//输出调用结果
System.out.println(objects[0].toString());
三、CXF和Spring整合服务端开发
1)新建web项目,并加入?相关包。(包含spring和cxf相关包) 2)写好一个接口和实现类。(见demo) 3)新建beans.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: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">
<!-- spring公布web服务配置 -->
<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="helloService" class="com.jaxb.service.HelloServiceImpl" /> <!--
<bean id="helloService" class="com.aegis.service.HelloServiceImpl" /> -->
<!-- endpoint 方式公布web服务和 server方式一样 --> <!--
<jaxws:endpoint id="helloServiceWs" address="/helloService" implementor="#helloService" />
--> <!-- 还有一种写法,建议不要用这样的方法 ,假设实现类有的属性要通过spring依赖注入的话, 这样的方法仅仅是简单的new个实现类,他的属性没有通过spring依赖注入给注入值 --> <!--
<jaxws:endpoint id="helloServiceWs" address="/helloService" implementor="com.jaxb.service.HelloServiceImpl" />
-->
<!—以下这个是wss4j的配置,后面会讲到相关知识,须要配置在spring配置文件里 --> <!-- wss4j 服务端配置 -->
<bean id="wss4jInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<constructor-arg>
<map>
<entry key="action" value=http://www.mamicode.com/"UsernameToken" />
<!-- <entry key="passwordType" value=http://www.mamicode.com/"PasswordText" /> -->
<entry key="passwordDigest" value=http://www.mamicode.com/"PasswordText" />
<entry key="passwordCallbackClass" value=http://www.mamicode.com/"com.security.service.ServerPasswordCallbackHandler" />
</map>
</constructor-arg>
</bean>
<jaxws:server id="helloServiceWs" address="/helloService"> <jaxws:serviceBean>
<ref bean="helloService" />
</jaxws:serviceBean><!-- 使用这样的方法公布web服务 --> <jaxws:inInterceptors>
<ref bean="wss4jInInterceptor" />
</jaxws:inInterceptors><!—wss4j配置 --> <!-- <jaxws:serviceFactory>
<ref bean="jaxWsServiceFactoryBean" />
</jaxws:serviceFactory> --><!—数据绑定方式配置 -->
</jaxws:server>
<!-- 通过Spring创建数据绑定的类-->
<!-- <bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding" /> <bean id="jaxWsServiceFactoryBean"
class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean"> <property name="wrapped" value=http://www.mamicode.com/"true" />
4)配置web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<!—在tomcat中公布须要配置servlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
5)公布web项目
由于在web.xml里面配置了servlet,则能够将项目公布到tomcat下,启动tomcat就可以。
6)訪问wsdl
http://localhost:8080/WSCXF/ws/helloService?wsdl
四、CXF和Spring整合client调用
1)新建一个webclient项目,用wsdl2java生成client代码。将生成的client代码复制到src下。加入?spring相关包。
2)配置spring配置文件。
beans.xml存放在src文件夹下,详细配置例如以下:
<?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">
<!-- wss4j 配置在client,后面有讲到相关知识 -->
<!-- wss4j client配置 --> <bean id="wss4jOutInterceptor"
class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value=http://www.mamicode.com/"UsernameToken" />
<entry key="user" value=http://www.mamicode.com/"Fetion" />
<!-- <entry key="passwordType" value=http://www.mamicode.com/"PasswordText" /> -->
<entry key="passwordDigest" value=http://www.mamicode.com/"PasswordText" />
<entry key="passwordCallbackClass" value=http://www.mamicode.com/"com.security.client.ClientPasswordCallbackHandler" />
</map>
</constructor-arg>
</bean>
<jaxws:client id="helloServeiceClient" address="http://localhost:8080/WSCXF/ws/helloService" serviceClass="com.jaxb.client.IHelloService">
<jaxws:outInterceptors>
<ref bean="wss4jOutInterceptor" />
</jaxws:outInterceptors><!-- wss4j client配置-->
</jaxws:client>
</beans>
2) 调用web服务 在main方法中执行:
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");//此处beans.xml放在src下,也须要放在其它文件夹下,但须要注明清楚
//获取webservice服务的操作接口
IHelloService helloService = (IHelloService)app.getBean("helloServeiceClient");
五、基于soap的web服务的WS-Security安全规范
很多其它请參考原文档:
http://wenku.baidu.com/link?url=MbPPOKCficQCAwSZduszpMFSD3f8xCKeNz6YUtwFS1TkHharz1aPPfkXD_n8vZZ4sBDjevk74jlp9fUZZ1DH9XcjrvqTsN747D-EtZPPNKm
webservice之cxf样例