Web Service:Apache CXF简单例子

service框架用apache CXF,服务器tomcat ,还有spring框架
OpenSource中关于CXF的简介:Apache CXF一个开源的Service框架,它实现了JCP与Web Service中一些重要标准。CXF简化了构造,集成,面向服务架构(SOA)业务组件与技术的灵活复用。在CXF中,Service使用WSDL标准定义并能够使用各种不同的消息格式(或binding)和网络协议(transports)包括SOAP、XML(通过HTTP或JMS)进行访问。CXF同样支持多种model 如:JAX-WS,JBI,SCA和CORBA service。CXF设计成可灵活部署到各种容器中包括Spring-based,JBI,SCA, Servlet和J2EE容器。

用CXF构建webservice程序的大致流程如下:

1 配置web.xml文件,加载CXF

2 编写java接口,和接口的实现

3 在spring的applicationContext.xml中配置java文件

4 创建beans.xml文件,里面配置接口的实现类

5 运行tomca服务器即可生成wsdl文件

6 编写测试代码:编写java测试文件和client-beans.xml文件

7编写运行客户端文件所需的服务器,可以单独编写,也可以在随tomcat启动

示例代码。首先下载cxf,然后解压,将lib下的所有jar包拷到工程下面

一 最简单的helloword

1 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">


<web-app>
<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>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>
2 接口HelloWord.java和接口的实现HelloWordImpl.java

package com.demo;

import javax.jws.WebService;

/** *//**
* service interface
*
* @author
*
*/
@WebService
public interface HelloWord ...{
String sayHi(String text);
}package com.demo;

import javax.jws.WebService;

/** *//**
* implementation
*
* @author
*
*/
@WebService
public class HelloWordImpl implements HelloWord ...{

public String sayHi(String text) ...{
return "Hi" + text;
}
}
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">

<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" />

<jaxws:endpoint id="helloWord"
implementor="com.demo.HelloWordImpl" address="/HelloWord" />

</beans>
4 启动tomcat服务器,在浏览器里面输入http://localhost:8888/helloword后页面上会出现

{http://demo.com/}HelloWordImplPort ,点击会看到生成的wsdl文件http://localhost:8888/helloword/HelloWord?wsdl

5 客户端Client.java和client-beans.xml

package com.demo;

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

public final class Client ...{

private Client() ...{

}

public static void main(String args[]) throws Exception ...{
// START SNIPPET: client
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] ...{ "com/demo/client-beans.xml" });

HelloWord client = (HelloWord) context.getBean("client");

String response = client.sayHi("+what????");
System.out.println("Response: " + response);
System.exit(0);
// END SNIPPET: client
}
}<?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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">

<bean id="client" class="com.demo.HelloWord"
factory-bean="clientFactory" factory-method="create" />

<bean id="clientFactory"
class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.demo.HelloWord" />
<property name="address"
value="http://localhost:9002/HelloWord" />
</bean>

</beans>
6 客户端运行时所需的服务器Server.java

package com.demo;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class Server ...{

protected Server() throws Exception ...{
System.out.println("Starting Server");

org.mortbay.jetty.Server server = new org.mortbay.jetty.Server();

SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(9002);
server.setConnectors(new Connector[] ...{connector});

WebAppContext webappcontext = new WebAppContext();
webappcontext.setContextPath("/");

webappcontext.setWar("WebRoot");

HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] ...{webappcontext, new DefaultHandler()});

server.setHandler(handlers);
server.start();
System.out.println("Server ready...");
server.join();
}

public static void main(String args[]) throws Exception ...{
new Server();
}

}
7 运行客户端Client.java的顺序,先启动tomcat,然后运行Server.java,最后运行Client.java

二 不单独编写Server.java文件,让其和tomcat一同启动.需要改动的地方

1 web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/applicationContext.xml
WEB-INF/beans.xml
</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-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>/webservice/*</url-pattern>
</servlet-mapping>

2 applicationContext.xml 配置文件中com.demo.HelloWordFind, HelloWordFind.java可以写一个查询表HELLO_WORD中所有内容的代码。相应的HelloWord.java和HelloWordImpl.java也需要修改

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>mapping/HelloWord.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>

<bean id="hellowordfind" class="com.demo.HelloWordFind">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>

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">

<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="hellowordimpl" class="com.demo.HelloWordImpl">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource"/>
<property name="hellowordfind" ref="hellowordfind" />
</bean>
<jaxws:endpoint id="helloword" implementor="#hellowordimpl" address="/hellword" />
</beans>
4 主要需要改动的已经完成。客户端代码变化不大。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qiuye402/archive/2008/01/15/2044933.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值