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

<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEweb-app
PUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.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 > CXFServlet </ 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;

/***/ /**
*serviceinterface
*
*
@author
*
*/

@WebService
public interface HelloWord ... {
StringsayHi(Stringtext);
}

package com.demo;

import javax.jws.WebService;

/***/ /**
*implementation
*
*
@author
*
*/

@WebService
public class HelloWordImpl implements HelloWord ... {

publicStringsayHi(Stringtext)...{
return"Hi"+text;
}

}

3 beans.xml

<? xmlversion = " 1.0 " encoding = " UTF-8 " ?>

< beansxmlns = " 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:endpointid = " 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 ... {

privateClient()...{

}


publicstaticvoidmain(Stringargs[])throwsException...{
//STARTSNIPPET:client
ApplicationContextcontext=newClassPathXmlApplicationContext(
newString[]...{"com/demo/client-beans.xml"});

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

Stringresponse
=client.sayHi("+what????");
System.out.println(
"Response:"+response);
System.exit(
0);
//ENDSNIPPET:client
}

}
<? xmlversion = " 1.0 " encoding = " UTF-8 " ?>

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

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

< beanid = " clientFactory "
class = " org.apache.cxf.jaxws.JaxWsProxyFactoryBean " >
< propertyname = " serviceClass " value = " com.demo.HelloWord " />
< propertyname = " 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 ... {

protectedServer()throwsException...{
System.out.println(
"StartingServer");

org.mortbay.jetty.Serverserver
=neworg.mortbay.jetty.Server();

SelectChannelConnectorconnector
=newSelectChannelConnector();
connector.setPort(
9002);
server.setConnectors(
newConnector[]...{connector});

WebAppContextwebappcontext
=newWebAppContext();
webappcontext.setContextPath(
"/");

webappcontext.setWar(
"WebRoot");

HandlerCollectionhandlers
=newHandlerCollection();
handlers.setHandlers(
newHandler[]...{webappcontext,newDefaultHandler()});

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


publicstaticvoidmain(Stringargs[])throwsException...{
newServer();
}


}

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 > CXFServlet </ 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也需要修改

<? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//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 >
<
分享到:
评论

你可能感兴趣的:(apache,spring,Web,servlet,webservice)