`

部署WebService服务(cxf,spring)

阅读更多
使用CXF,spring部署Web Service服务

server 实现

步骤一 新建Web工程

步骤二 下载cxf包(地址:http://www.apache.org/dyn/closer.cgi?path=/cxf/2.6.2/apache-cxf-2.6.2.tar.gz)

步骤三 解压cxf包,将apache-cxf-*.*.*/lib目录下的jar包复制到Web工程->WebRoot->WEB-INF->lib文件夹下(cxf已经整合了spring所以不需要在导入spring的包了)。不必导入全部jar包 具体见附件图片

步骤四 在Web工程下创建一个接口并定义一个方法
例:
package com.cxf;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface ISayHello
{
    public String sayHello(@WebParam(name="name")String name);
}

步骤五 新建一个类继承接口
例:
package com.cxf;
import javax.jws.WebService;

@WebService(endpointInterface = "com.cxf.ISayHello")
public class SayHelloImpl implements ISayHello
{
    @Override
    public String sayHello(String name)
    {
        return "my name is " + name;
    }
}

步骤六 在工程src目录下创建spring配置文件applicationContext.xml
applicationContext.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-servlet.xml" />

<bean id="sayHelloImpl" class="com.cxf.SayHelloImpl" />
<jaxws:endpoint id="sayHello" implementor="#sayHelloImpl" address="/sayHello" />
</beans>

步骤七 修改Web工程->WebRoot->WEB-INF->web.xml
在web.xml文件<web-app></web-app>中加上:
<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
          classpath:applicationContext.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工程
打开浏览器访问:http://localhost:8080/WebService/




client 实现


步骤一 在工程中新建一个类(可以重新建一个工程但需要导入cxf的相关包)
例:
package com.cxf.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cxf.ISayHello;

public class CxfClient
{
    public static void main(String[] args)
    {
        ClassPathXmlApplicationContext context
        = new ClassPathXmlApplicationContext(new String[] {"com/cxf/client/applicationContext_client.xml"});

    ISayHello client = (ISayHello)context.getBean("sayHello2");

    String response = client.sayHello("Joe");
    System.out.println("Response: " + response);
    System.exit(0);
    }
}

步骤二 在client同级目录新建client spring配置文件applicationContext_client.xml
applicationContext_client.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
<!--
该方式访问ws服务也是利用spring的依赖注入法
id是spring IOC容器唯一标识符
serviceClass是webservices服务接口
address是服务wsdl地址
-->
    <jaxws:client id="sayHello2" serviceClass="com.cxf.ISayHello" address="http://localhost:8080/WebService/sayHello?wsdl" />
</beans>

执行CxfClient的main方法
控制台打印
Response: my name is Joe


在完成实例中所遇问题

启动tomcat抛:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sayHello': Invocation of init method failed; nested exception is java.lang.LinkageError: JAXB 2.1 API is being loaded from the bootstrap classloader, but this RI (from jar:file:/D:/apache-tomcat-6.0.29/webapps/WebService/WEB-INF/lib/jaxb-impl-2.2.4-1.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) needs 2.2 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (See http://java.sun.com/j2se/1.6.0/docs/guide/standards/)
Caused by: java.lang.LinkageError: JAXB 2.1 API is being loaded from the bootstrap classloader, but this RI (from jar:file:/D:/apache-tomcat-6.0.29/webapps/WebServiceCXF/WEB-INF/lib/jaxb-impl-2.2.4-1.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) needs 2.2 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (See http://java.sun.com/j2se/1.6.0/docs/guide/standards/)

问题原因:cxf与jdk jar包冲突
解决方法:
1.升级JDK到jdk1.6.0_25(据说jdk1.6.0_25后就支持了),然后修改tomcat的执行jdk版本(还有另一种方法,可以google下)


问题二:
执行客服端类main方法时抛:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [demo/spring/client/client-beans.xml]; nested exception is javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found

问题原因:
是我使用MyEclipse创建项目时引用了J2EE 1.4 Library Container库

解决方法:
1.删掉J2EE 1.4 Library 。
2.换为JAVA EE 5/6 Library




  • 大小: 47.2 KB
分享到:
评论

相关推荐

    webservice(cxf)与spring整合源码+文档

    webservice与spring整合学习文档,里面的项目部署上就可以使用

    WebService_CXF实现及ANT

    CXF对Interceptor拦截器的支持 CXF WebService中传递复杂类型对象 CXF整合Spring ANT工具快速构建、部署工程

    用cxf开发webservice

    Apache CXF是一个开源的Service框架,它实现了JCP与Web Service中一些重要标准。CXF简化了构造,集成,面向服务架构(SOA)业务组件...CXF设计成可灵活部署到各种容器中包括Spring-based,JBI,SCA, Servlet和J2EE容器。

    WebService之CXF开发指南

    一、Web Services、SOA简介。 二、CXF简介。 1、关于ApacheCXF。 2、功能特性。 3、CXF安装包下载及目录结构。 ...d、spring配置。 e、web应用配置。 f、应用部署。 g、启动服务。 h、消费服务。】

    Spring+CXF开发WebService

    使用 CXF 做 webservice 简单例子...它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。

    CXF+spring+jdk1.5开发webService

    cxf+spring+jdk1.5开发webService部署weblogic9.2

    使用CXF結合spring实现的WebService例子

    简单的CXF(WebService)例子 打包后文件太大,故所需的jar文件自己下载 cxf版本:cxf-2.2.7 spring版本:spring2.5.6 例子简单,使用,部署即可运行。

    cxf做webservice接口

    Apache CXF 是一个开放源代码框架...它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。

    使用CXF+spring创建一个web的接口项目源代码

    使用CXF+spring创建一个web的接口项目源代码,Myeclipse创建,部署到tomcat中即可访问。 与此对应的博客地址为:http://blog.csdn.net/zxnlmj/article/details/28880303

    使用 CXF 做 webservice 简单例子 - 烽火编程 - 博客园

    Apache CXF 是一个开放源代码框架...它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。

    使用 CXF 做 webservice 简单例子

    apache CXF 是一个开放源代码框架...它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。

    spring-boot-cxf-jaxrs:使用Spring Boot和CXF JAXRS快速入门

    本示例演示了如何基于将Apache CXF与Spring Boot结合使用。 快速入门使用Spring Boot来配置一个小的应用程序,其中包括启用了Swagger的CXF JAXRS端点。 重要的 该快速入门可以在2种模式下运行:在您的计算机和...

    cxf webservice 实例(包括服务端及客户端代码),可用

    cxf webservice 实例(包括服务端及客户端代码),亲测可用,项目采用spring与cxf结合的,附件解压后看到两个项目,分部是服务端项目和客户端项目,启动tomcat服务器部署后,通过客户端右键运行java 的 mian方法代码...

    webService

    cxf webservice 实例(包括服务端及客户端代码),亲测可用,项目采用spring与cxf结合的,附件解压后看到两个项目,分部是服务端项目和客户端项目,启动tomcat服务器部署后,通过客户端右键运行java 的 mian方法代码...

    apache-cxf-3.1.10.zip

    Apache CXF 是一个开放源代码框架...它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。

    tutorial-soap-spring-boot-cxf:教程如何使用Spring Boot和Apache CXF创建,测试,部署,监视SOAP-Webservices

    tutorial-soap-spring-boot-cxf 教程如何使用 , 和创建,测试,部署,监视SOAP 接下来的每个步骤都基于前一个步骤。 因此,如果从第3步开始,则代码中将覆盖第1步和第2步。 步骤1-3:随博客文章一起发布: (或...

    spring-cxf-webservices

    使用 spring 的 CXF Web 服务示例 使用 spring-boot 的 CXF Web 服务示例 调用上面两个网络服务的客户端 要生成客户端使用的类,请使用 mvn generate-sources(在 /target/generated 中生成的@WebServiceClient 类...

    Java源码 SpringMVC Mybatis Shiro Bootstrap Rest Webservice

    2. Wink Rest服务、Webservice服务:jaxws、CXF等 3. IO 流上传下载文件,多线程操作 4. 发送邮件,配置邮件服务器,发基于html、纯文本格式的邮件(可以免费赠送网络爬虫,使其群发邮件,做到广告推送等) 5. MD...

    JavaEE求职简历-姓名-JAVA开发工程师.doc

    6.能够使用WebService、CXF技术,完成第三方服务开发; 7.熟练使用SVN和Git进行代码的版本控制; 8.熟练使用Maven来进行多模块项目的构建以及管理项目中依赖包,以及使用Maven最终完成项目的热部署; 9.熟悉junit技术...

Global site tag (gtag.js) - Google Analytics