Web服务cxf框架发布

CXF创建webservice客户端和服务端

分类:             【Webservice】              2013-07-13 18:42     11645人阅读     评论(10)     收藏     举报

Web服务cxf框架发布

目录(?)[+]

  1. 一CXF的介绍
  2. 二CXF的准备条件
  3. 三创建webservice服务端
    1. 先将jar包放入lib目录
    2. 在webxml中配置CXF框架的核心servlet
    3. 在applicationContextxml中导入xml并且发布webservice服务
    4. 定义webservice接口FacelookWebService 和 实现类FacelookWebServiceImpl
    5. 保存代码发布项目启动tomact
    6. 通过客户端调用服务端webservice

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本人声明。否则将追究法律责任。 作者:永恒の_☆ 地址:http://blog.csdn.net/chenghui0317/article/details/9320053

一、CXF的介绍

Apache CXF是一个开源的WebService框架,CXF大大简化了Webservice的创建,同时它继承了XFire的传统,一样可以和spring天然的进行无缝的集成。CXF框架是一种基于servlet技术的SOA应用开发框架,要正常运用基于CXF应用框架开发的企业应用,除了CXF应用本身之外,还需要JDK和servlet容器的支持。

二、CXF的准备条件

所需要的jar包:

xmlbeans-2.4.0.jar

wss4j-1.5.9.jar

jetty-server-7.1.6.v20100715.jar

jetty-util-7.1.6.v20100715.jar

geronimo-ws-metadata_2.0_spec-1.1.3.jar

geronimo-activation_1.1_spec-1.1.jar

geronimo-servlet_3.0_spec-1.0.jar

velocity-1.6.4.jar

jaxb-xjc-2.2.1.1.jar

xml-resolver-1.2.jar

wsdl4j-1.6.2.jar

cxf-2.3.0.jar

XmlSchema-1.4.7.jar

jaxb-api-2.2.1.jar

jaxb-impl-2.2.1.1.jar

neethi-2.0.4.jar

geronimo-annotation_1.0_spec-1.1.1.jar

geronimo-stax-api_1.0_spec-1.0.1.jar

下载地址:http://download.csdn.net/detail/ch656409110/5748183   (取自己需要的jar包)

三、创建webservice服务端

1、先将jar包放入lib目录

2、在web.xml中配置CXF框架的核心servlet

[html] view plaincopyprint?

  1. <!-- CXF -->
  2. <servlet>
  3. <servlet-name>CXFServlet</servlet-name>
  4. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  5. <load-on-startup>1</load-on-startup>
  6. </servlet>
  7. <servlet-mapping>
  8. <servlet-name>CXFServlet</servlet-name>
  9. <url-pattern>/services/*</url-pattern>
  10. </servlet-mapping>
	<!-- CXF -->
	<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>/services/*</url-pattern>
	</servlet-mapping>

3、在applicationContext.xml中导入xml,并且发布webservice服务。

[html] view plaincopyprint?

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xmlns:jaxws="http://cxf.apache.org/jaxws"
  9. xmlns:jaxrs="http://cxf.apache.org/jaxrs"
  10. xsi:schemaLocation="http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  14. http://www.springframework.org/schema/aop
  15. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  16. http://cxf.apache.org/jaxws
  17. http://cxf.apache.org/schemas/jaxws.xsd
  18. http://cxf.apache.org/jaxrs
  19. http://cxf.apache.org/schemas/jaxrs.xsd
  20. "
  21. default-autowire="byName"
  22. >
  23. <import resource="classpath:META-INF/cxf/cxf.xml" />
  24. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  25. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  26. <!-- <jaxws:endpoint id="facelookWebService" address="/facelookWebService" implementor="com.facelook.webservice.server.FacelookWebServiceImpl"></jaxws:endpoint> -->
  27. <!-- 不知道为什么,这里的webservice配置,只能用bean来实现,否则 注入的service为空。但是之前有个项目却可以,百思不得其解。。 -->
  28. <bean id="facelookWebService" class="com.facelook.webservice.server.FacelookWebServiceImpl"/>
  29. <jaxws:endpoint id="facelookWebService1" address="/facelookWebService" implementorClass="com.facelook.webservice.server.FacelookWebServiceImpl">
  30. <jaxws:implementor ref="facelookWebService"/>
  31. </jaxws:endpoint>
  32. </beans>
<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
						http://cxf.apache.org/jaxws
					    http://cxf.apache.org/schemas/jaxws.xsd
					    http://cxf.apache.org/jaxrs
					    http://cxf.apache.org/schemas/jaxrs.xsd
					    "
						default-autowire="byName"
						>
    <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="facelookWebService" address="/facelookWebService" implementor="com.facelook.webservice.server.FacelookWebServiceImpl"></jaxws:endpoint> -->
    <!-- 不知道为什么,这里的webservice配置,只能用bean来实现,否则 注入的service为空。但是之前有个项目却可以,百思不得其解。。 -->
    <bean id="facelookWebService" class="com.facelook.webservice.server.FacelookWebServiceImpl"/>
    <jaxws:endpoint id="facelookWebService1" address="/facelookWebService" implementorClass="com.facelook.webservice.server.FacelookWebServiceImpl">
    	<jaxws:implementor ref="facelookWebService"/>
    </jaxws:endpoint>
</beans>

4、定义webservice接口FacelookWebService 和 实现类FacelookWebServiceImpl。

[java] view plaincopyprint?

  1. @WebService
  2. public interface FacelookWebService {
  3. /**
  4. * 根据传递的条件获取相册信息
  5. * xml的格式规范
  6. * <?xml version=\"1.0\" encoding=\"UTF-8\"?>
  7. * <facelook>
  8. *  <condition>
  9. *      <name></name>
  10. *      <description></description>
  11. *      <pageno></pageno>
  12. *      <pagesize></pagesize>
  13. *  </condition>
  14. * </facelook>
  15. * 这里的WebParam必须指定,否则调用的时候返回null
  16. * @return
  17. */
  18. public String getAlbumList(@WebParam(name="xmlStr") String xmlStr);
  19. }
  20. @WebService
  21. //这后面的可以不写注释后面的配置,在applicationContext配置也一样(serviceName="facelookWebService",endpointInterface="com.facelook.webservice.server.FacelookWebService")
  22. public class FacelookWebServiceImpl implements FacelookWebService{
  23. @Autowired
  24. private AlbumService albumService;
  25. @Override
  26. public String getAlbumList(String xmlStr) {
  27. try {
  28. List<Album> albumList = getAlbumPage(xmlStr);
  29. JSONArray jsonArray = JSONArray.fromObject(albumList);
  30. return jsonArray.toString();
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. return null;
  35. }
  36. }
@WebService
public interface FacelookWebService {

	/**
	 * 根据传递的条件获取相册信息
	 * xml的格式规范
	 * <?xml version=\"1.0\" encoding=\"UTF-8\"?>
	 * <facelook>
	 * 	<condition>
	 * 		<name></name>
	 * 		<description></description>
	 * 		<pageno></pageno>
	 * 		<pagesize></pagesize>
	 * 	</condition>
	 * </facelook>
	 * 这里的WebParam必须指定,否则调用的时候返回null
	 * @return
	 */
	public String getAlbumList(@WebParam(name="xmlStr") String xmlStr);
}

@WebService
//这后面的可以不写注释后面的配置,在applicationContext配置也一样(serviceName="facelookWebService",endpointInterface="com.facelook.webservice.server.FacelookWebService")
public class FacelookWebServiceImpl implements FacelookWebService{

    @Autowired
    private AlbumService albumService;
    
    @Override
    public String getAlbumList(String xmlStr) {
        try {
            List<Album> albumList = getAlbumPage(xmlStr);
            JSONArray jsonArray = JSONArray.fromObject(albumList);
            return jsonArray.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

这样子,基本上就可以了。

5、保存代码,发布项目,启动tomact。

在地址栏输入:http://localhost:8080/house/services/houseWebService?wsdl  即可看到发布的服务端的明细。

显示如下:

这就表示CXF发布的webservice服务端成功了。

6、通过客户端调用服务端webservice。

axis的客户端访问:

[java] view plaincopyprint?

  1. public static void main(String[] args) throws ServiceException, RemoteException, MalformedURLException {
  2. String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  3. + "     <facelook>"
  4. + "        <condition>"
  5. + "            <name>家</name>"
  6. + "            <description></description>"
  7. + "            <pageno></pageno>"
  8. + "            <pagesize></pagesize>"
  9. + "        </condition>"
  10. + "     </facelook>";
  11. Service service = new Service();
  12. Call call = (Call) service.createCall();
  13. call.setTargetEndpointAddress("http://localhost:8080/facelook/services/facelookWebService?wsdl");
  14. QName qName = new QName("http://server.webservice.facelook.com/", "getAlbumList");
  15. call.setOperationName(qName);
  16. call.setUseSOAPAction(true);
  17. //这下面两行一定要加上,否则接收在服务器端收不到。
  18. call.addParameter("xmlStr", XMLType.XSD_STRING, ParameterMode.IN);
  19. call.setReturnType(XMLType.XSD_STRING);
  20. String result = (String) call.invoke(new Object[] { xmlStr });
  21. System.out.println(result);
  22. //将返回的字符串转换成list集合
  23. //JSONArray array = JSONArray.fromObject(result);
  24. //List<Album> list = JSONArray.toList(array,Album.class);
  25. }
	public static void main(String[] args) throws ServiceException, RemoteException, MalformedURLException {
		String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				 + " 	 <facelook>"
				 + "	 	<condition>"
				 + " 	 		<name>家</name>"
				 + "			<description></description>"
				 + "			<pageno></pageno>"
				 + "			<pagesize></pagesize>"
				 + "  		</condition>"
				 + "	 </facelook>";

		  Service service = new Service();
		  Call call = (Call) service.createCall();
		  call.setTargetEndpointAddress("http://localhost:8080/facelook/services/facelookWebService?wsdl");
		  QName qName = new QName("http://server.webservice.facelook.com/", "getAlbumList");
		  call.setOperationName(qName);
		  call.setUseSOAPAction(true);
		  //这下面两行一定要加上,否则接收在服务器端收不到。
		  call.addParameter("xmlStr", XMLType.XSD_STRING, ParameterMode.IN);
		  call.setReturnType(XMLType.XSD_STRING);
		  String result = (String) call.invoke(new Object[] { xmlStr });
		  System.out.println(result);

		//将返回的字符串转换成list集合
		//JSONArray array = JSONArray.fromObject(result);
		//List<Album> list = JSONArray.toList(array,Album.class);

	}

CXF客户端访问:

[java] view plaincopyprint?

  1. public static void main(String[] args) throws Exception {
  2. //这个是用cxf 客户端访问cxf部署的webservice服务
  3. //千万记住,访问cxf的webservice必须加上namespace ,否则通不过
  4. //现在又另外一个问题,传递过去的参数服务端接收不到
  5. JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
  6. org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8080/facelook/services/facelookWebService?wsdl");
  7. //url为调用webService的wsdl地址
  8. QName name=new QName("http://server.webservice.facelook.com/","getAlbumList");
  9. //namespace是命名空间,methodName是方法名
  10. String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  11. + "     <facelook>"
  12. + "        <condition>"
  13. + "            <name>家</name>"
  14. + "            <description></description>"
  15. + "            <pageno></pageno>"
  16. + "            <pagesize></pagesize>"
  17. + "        </condition>"
  18. + "     </facelook>";
  19. //paramvalue为参数值
  20. Object[] objects=client.invoke(name,xmlStr);
  21. //调用web Service//输出调用结果
  22. System.out.println(objects[0].toString());
  23. }
public static void main(String[] args) throws Exception {
		//这个是用cxf 客户端访问cxf部署的webservice服务
		//千万记住,访问cxf的webservice必须加上namespace ,否则通不过
		//现在又另外一个问题,传递过去的参数服务端接收不到
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8080/facelook/services/facelookWebService?wsdl");
		//url为调用webService的wsdl地址
		QName name=new QName("http://server.webservice.facelook.com/","getAlbumList");
		//namespace是命名空间,methodName是方法名
		String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				 + " 	 <facelook>"
				 + "	 	<condition>"
				 + " 	 		<name>家</name>"
				 + "			<description></description>"
				 + "			<pageno></pageno>"
				 + "			<pagesize></pagesize>"
				 + "  		</condition>"
				 + "	 </facelook>";
		//paramvalue为参数值
		Object[] objects=client.invoke(name,xmlStr);
		//调用web Service//输出调用结果
		System.out.println(objects[0].toString());
}

在这里面传递的xml规范由 服务端自己规范好了,然后去解析、获取参数,执行相应的操作,返回想要的结果给调用的客户端。。

时间: 2024-08-27 21:48:37

Web服务cxf框架发布的相关文章

Web服务cxf框架发布2

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任. 作者:永恒の_☆ 地址:http://blog.csdn.net/chenghui0317/article/details/9320053 一.CXF的介绍 Apache CXF是一个开源的WebService框架,CXF大大简化了Webservice的创建,同时它继承了XFire的传统,一样可以和spring天然的进行无缝的集成.CXF框架是一种基于servlet技术的SOA应用开发框架

用cxf 框架发布webService(第二种,ServerFactoryBean带接口)

接口类需要注解 可以添加输入输出拦截器 package cn.itcast.cxf.server; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import cn.itcast.webservice.userSer

CXF框架介绍及Spring集成

1.CXF框架概念介绍 Apache CXF 是一个开源的 WebService 框架,CXF可以用来构建和开发 WebService,这些服务可以支持多种协议,比如:SOAP.POST/HTTP.HTTP ,CXF 大大简化了WebService并且可以天然地和 Spring 进行无缝集成.CXF是 Celtrix (ESB框架)和 XFire(webserivice) 合并而成,核心是org.apache.cxf.Bus(总线),类似于Spring的 ApplicationContext,C

使用RpcLite构建SOA/Web服务

使用RpcLite构建SOA/Web服务 SOA框架系列 1. 使用RpcLite构建SOA/Web服务 提到Web服务最先想到的就是WebService此外常用的还有WCF.ServiceStack.WebApi等. RpcLite是一个开源的轻量级SOA服务框架,除了基本的提供Web服务还包括SOA治理系统(本文暂不展开在后继的文章会详细说明).RpcLite与WCF比较像,下面介绍使用方法. 1.   创建一个Web项目,Framework选择>=4.0 2.   从NuGet中添加Rpc

Eclipse+CXF框架开发Web服务实战

一. 说明 采用CXF框架开发webservice. 所用软件及版本如下. ? 操作系统:Window XP SP3. ? JDK:JDK1.6.0_07,http://www.oracle.com/technetwork/java/javase/downloads/index.html. ? Tomcat:apache-tomcat-6.0.14.exe,http://tomcat.apache.org/. ? IDE:eclipse-jee-juno-SR1-win32.zip,http:/

用CXF发布Web服务

1.下载apache-cxf-2.7.6jar包,并把lib目录下的所有jar包导入项目 2.编写测试的实体类,示例如下: 1 package cn.bd.weather.entity; 2 3 import java.util.Date; 4 5 import javax.xml.bind.annotation.XmlRootElement; 6 /** 7 * 8 * @author Administrator 9 * @XmlRootElement 表示根元素 10 */ 11 @XmlR

使用CXF框架,发布webservice服务,并使用客户端远程访问webservice

使用CXF框架,发布webservice服务,并使用客户端远程访问webservice  1. CXF介绍 :soa的框架    * cxf 是 Celtrix (ESB框架)和 XFire(webserivice) 合并而成,并且捐给了apache      * CxF的核心是org.apache.cxf.Bus(总线),类似于Spring的 ApplicationContext    * CXF默认是依赖于Spring的    * Apache CXF 发行包中的jar,如果全部放到lib中

利用jws发布一个查询员工信息的Web服务(员工信息存储在数据库中)

这是<基于服务的软件系统>的课程设计: 一.作业要求 编写查询员工信息的Web服务(员工信息存储在数据库中).第一个Web服务:输入员工号,返回该员工号的员工的基本信息,包括员工号.员工名称.所在部门.出生日期.职位.职称.入职日期等信息.第二个Web服务:输入部门.职称,返回该部门具有该职称的所有员工的基本信息,员工基本信息与上面相同.分别针对上述两个Web服务,分别编写调用这两个Web服务的程序(或网页).要求在输入界面上输入待查询数据,调用Web服务,并将Web服务返回的员工信息查询结果

Web Service (二) CXF自动发布Web Service(No Spring)

Web Service实现目前流行的框架主要有两种,cxf和axis这两个框架,下面是这两个框架的优缺点,我们这个项目中使用的是cxf这个框架,首先看一下没有集成spring的时候是怎么实现远程调用的. Axis与Cxf比较 在SOA领域,我们认为Web Service是SOA体系的构建单元(building block).这两个框架 都是从已有的开源项目发展起来的.这两个框架哪一个更好一些呢? 通过一个比较矩阵来比较Axis2和CXF变得有现实的意义.最主要的区别在以下几个方面: 1.CXF支