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
本文介绍如何使用Apache CXF框架在Tomcat服务器环境下搭建Web Service应用,并提供了从配置到测试的完整示例代码。
165

被折叠的 条评论
为什么被折叠?



