web service开发框架
来源:互联网 发布:no sleep mac下载 编辑:程序博客网 时间:2024/06/12 01:38
服务器端:
CXF的前身是Xfire,具体内容可参照http://xfire.codehaus.org/ 。IDE为Eclipse 3.2 (JDK 1.5以上)
1、 首先访问http://incubator.apache.org/cxf/ 下载所需的jar包。
2、 新建一个web工程,将下载的CXF的开发包加入到lib中。
3、 在web.xml 文件中添加提供web service的监听servlet,对应的处理类名为org.apache.cxf.transport.servlet.CXFServlet,并设置其启动时装载顺序属性为1;指定访问servlet的URL格式。
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>WSDemo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-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>
4、 新建一个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:p="http://www.springframework.org/schema/p"xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:cxf="http://cxf.apache.org/core"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><bean id="ws" class="org.han.websevices.NotifyServiceImpl"/><jaxws:endpoint id="helloWorld" implementor="#ws" address="/ws" /></beans>
5、 编写要发布为web service的类和接口,注意注解(annotation)的使用。在如上的配置文件中,我们指定了要发布为web service的类名,访问地址,访问端点标识。
package org.han.websevices;import javax.jws.WebService;@WebServicepublic interface INotifyService {public void sayHello(String name);}package org.han.websevices;import javax.jws.WebService;@WebService(endpointInterface="org.han.websevices.INotifyService")public class NotifyServiceImpl implements INotifyService {@Overridepublic void sayHello(String name) {// TODO Auto-generated method stubSystem.out.println(name);}}
6、 发布web应用到对应的web容器(例如tomcat5.5)或者用内嵌的Jetty6.0进行部署。
访问:http://localhost:8080/WSDemo/ws?wsdl 如果能正确显示xml文件则说明部署成功。
客户端:
1.新建一个java项目,也导入该包
2.新建客户程序(如果熟悉cxf可以运用cxf的wsdl2java命令直接根据wsdl生成)
package org.han.test;import org.han.websevices.INotifyService;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); INotifyService client = (INotifyService) ctx.getBean("client"); client.sayHello("hello"); }}
根据代码,我们还需要新建一个INotifyService接口,这是服务器端暴露的服务,代码和服务器端一致即可。 配置spring配置文件:
<?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:p="http://www.springframework.org/schema/p"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"><bean id="client" class="org.han.websevices.INotifyService" factory-bean="clientFactory"factory-method="create" /><bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"><property name="serviceClass" value="org.han.websevices.INotifyService" /><property name="address" value="http://localhost:8080/WSDemo/ws" /></bean></beans>注意:<property name="address" value="http://localhost:8080/WSDemo/ws" />中的/ws要与服务器端beans.xml中的<jaxws:endpoint id="helloWorld" implementor="#ws" address="/ws" />的address属性对应。
大致需要的一些jar:
补充:
2.控制台报错整理:
严重: Servlet.service() for servlet CXFServlet threw exception
java.lang.NoClassDefFoundError: org/apache/xml/serializer/TreeWalker
解决:导入serializer-2.7.1.jar 。
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorld': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/wsdl/extensions/ElementExtensible
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.apache.cxf.binding.soap.SoapTransportFactory' defined in class path resource [META-INF/cxf/cxf-extension-soap.xml]: Unsatisfied dependency expressed through constructor argument with index 2 of type [java.lang.String]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
解决:导入:neethi-3.0.1.jar 。
几种常用web service开发框架的比较:http://gaoge2000.blog.hexun.com/15987048_d.html
- web service开发框架
- Axis框架开发web service
- axis1.x框架开发web service
- Web Service 框架介绍
- Jersey : Java规范下REST风格Web Service开发框架
- JAVA开发Web Service几种框架介绍
- Spring Boot、Mybatis框架整合开发Java RESTful Web Service
- JAVA开发Web Service几种框架介绍
- JAVA开发Web Service几种框架介绍
- JAVA开发Web Service几种框架介绍
- JAVA开发Web Service几种框架介绍
- JAVA开发Web Service几种框架介绍
- JAVA开发Web Service几种框架介绍
- JAVA开发Web Service几种框架介绍
- JAVA开发Web Service几种框架介绍
- JAVA开发Web Service几种框架介绍
- JAVA开发Web Service几种框架介绍
- JAVA开发Web Service几种框架介绍
- sqlserver2005 joins
- 用 栈结构 实现反序读文件。
- _线程间通信
- mod_rewrite
- 设计模式之装饰
- web service开发框架
- 设计模式——Iterator模式实现研究
- 源码结构
- x264中x264_cpu_detect函数欣赏
- cocos2d-x SimpleGame入门例程笔记
- LINQ to SQL语句(1)之Where
- android in practice_create and start a service(portfolio project)
- 八大經方治感冒
- LINQ to SQL语句(2)之Select/Distinct