使用cxf创建webservice

1、   创建web项目。

2、   引入spring,cxf包。

3、  创建一个服务的终端接口(SayHello)

package zxf.cxf.service;

import javax.jws.WebMethod;

import javax.jws.WebService;

 @WebService

public interface  SayHello {

     @WebMethod

     public String say(String name);

}

4、  创建服务实现类(SayHello)

package zxf.cxf.service;

 

public class SayHelloImpl implements SayHello {

@Override

       public String say(String name) {

              System.out.println("hello");

              return "hello "+name;

       }

}

5、  创建服务器bean的配置文件(bean.xml)

<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-3.0.xsd

                                          http://cxf.apache.org/jaxws

                                          http://cxf.apache.org/schemas/jaxws.xsd ">

       <!--下面的配置是为了创建cxf需要的bean对象  -->

       <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终端 -->

       <jaxws:endpoint id="helloService" implementor="zxf.cxf.service.SayHelloImpl"

              address="/sayHello" />

</beans>

6、  配置web文件(配spring的监听器和cxf的servlet)

<?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>cxf_service</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>

  <!-- 通过上文参数指定spring配置文件的位置 -->

       <context-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>classpath:beans.xml</param-value>

       </context-param>

      

       <!-- 配置spring上下文载入器监听器,确保web服务器启动时,通知完成spring容器的初始化. -->

       <listener>

       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

       </listener>

       <!-- 配置cxf引擎的servlet -->

       <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>

7、  部署web服务到tomcat中去。查看服务文档。

8、  查看webservice的描述文档http://localhost:8080/cxf_service/helloService?wsdl

9、  创建webservice的克服端程序。

(1)       引入类库。Cxf+Spring

(2)       创建Spring配置文件clent.xml

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

       <jaxws:client id="sayHelloServiceClient"

              serviceClass="zxf.cxf.service.SayHello"

              address="http://localhost:8080/cxf_service/helloService" />

</beans>

(3)将sayHello接口复制过来。

(4)新建app客户端程序

package zxf.cxf.service;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

       public static void main(String[] args) {

              System.out.println(System.getProperty("java.endorsed.dirs"));

              ApplicationContext ac=new ClassPathXmlApplicationContext("client.xml");

              SayHello s=(SayHello)ac.getBean("sayHelloServiceClient");

              System.out.println(s.say("dfdsf"));

       }

}

(5)运行App程序如果出现下面错误

java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader, but this RI ***needs 2.1 API.

解决办法:
    1、如果是application工程,则在程序中打印出

         system.out.println(System.getProperty("java.endorsed.dirs"));
        
找到相应位置,将jaxb-api-2.1.jarjaxb-impl-2.1.12.jar放到对应目录下即可。没有endorsed目录,自己建一个。
    2、如果是java web项目,则在一个servlet之类的运行起来的项目中,打印:

         system.out.println(System.getProperty("java.endorsed.dirs"));
        
找到相应位置,将jaxb-api-2.1.jarjaxb-impl-2.1.12.jar放到对应目录下即可。没有endorsed目录,自己建一个。
         我的WEB项目中,打印出来的路径是:E:\Java\apache-tomcat-7.0.19\endorsed

endorsed目录里面放置的jar文件,将有覆盖系统API的功能。可以牵强的理解为,将自己修改后的API打入到虚,拟机指定的启动API中,取而代之。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值