CXF3.0.2+Spring3.2.14 Web Service入门实例二

离上次学习CXF的时间比较长了,最近一直在忙别的项目,没有时间再继续学习CXF,也就没有再写博客了!马上就要2016年了,今天有点时间,再写一篇,把CXF3.0.7与spring3.2.14整合写出来。本来应该写apache-cxf-3.1.4与spring3整合的,把apache-cxf-3.1.4解压出来以后,发现lib文件夹内是spring4,怕与项目引起冲突,故又换成了apache-cxf-3.0.7,特此说明!还是重头做起,看demo,搜技术博客!可能有原创的,也可能是有用Maven的,也可能是开发环境不一样,也有好多是转载的,结果我了了个去,死活出不来,各种大坑,小坑,连环坑!反正就是不出来,没办法一点一点来吧!介绍一下开发环境,jdk1.6.0_43+Tomcat6.0.29+ MyEclipse10.5,没有使用Maven进行管理!为了不出现一些不必要的错误,将所有的jar都导入了,当然实际开发过程中不会这样做的,下面也会有介绍的。一、新建web工程,注意红色的选择,在下面的工程实际上是mywbs3,mywbs9只是为演示用!

 

二、导入jar包

三、新建HelloWorld.java

package com.firstws.test;

 

import javax.jws.WebService;

 

@WebService

public interface HelloWorld {

    public String sayHi(String name);

}

四、新建实现类HelloWorldImpl.java

package com.firstws.test;

 

import javax.jws.WebService;

 

@WebService(endpointInterface ="com.firstws.test.HelloWorld")

public class HelloWorldImpl implements HelloWorld {

 

    @Override

    public String sayHi(String text) {

        System.out.println("sayHi called");

        return"Hello " + text;

    }

 

}

 

 

五、在Web.xml中添加

  <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>CXFService</servlet-name>

    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>CXFService</servlet-name>

    <url-pattern>/*</url-pattern>

  </servlet-mapping>

六、在src目录下新建applicationContext.xml

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

<beansxmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:jaxws="http://cxf.apache.org/jaxws"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsd

    http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd

    http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd

    http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsd

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

   

    <importresource="classpath:META-INF/cxf/cxf.xml"/>

    <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>

   

    <beanid="hello"class="com.firstws.test.HelloWorldImpl"/>

    <jaxws:endpointid="helloWorld"implementor="#hello"address="/HelloWorld"/>

 

    <beanid="myhw"class="com.firstws.test.HelloWorld"factory-bean="clientFactory"factory-method="create"/>

    <beanid="clientFactory"class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">

        <propertyname="serviceClass"value="com.firstws.test.HelloWorld"/>

    <propertyname="address"value="http://localhost:8080/mywbs3/HelloWorld"/>

    </bean>

   

</beans>

 

七、在浏览器中输入http://localhost:8080/mywbs3,不出现错误的话将会显示:


八、如果将web.xml中紫色的部分改为<url-pattern>/aaa/*</url-pattern>,那么在浏览器则要输入http://localhost:8080/mywbs3/aaa

九、注意applicationContext.xml中红色部分,在浏览器中输入

http://localhost:8080/mywbs3/HelloWorld?wsdl,正确的话将会出现


如果将红色的HelloWorld改为HelloWorld1,则浏览器要改为相应的

http://localhost:8080/mywbs3/HelloWorld1?wsdl

十、编写调用类,网上有好多种方法,先写三种吧,新建Client.java没有使用spring.(第一种)

package com.firstws.client;

 

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

 

import com.firstws.test.HelloWorld;

 

public class Client{

    /**

    private static final QName SERVICE_NAME = newQName("http://test.firstws.com/", "HelloWorld");

    private static final QName PORT_NAME = newQName("http://test.firstws.com/", "HelloWorldPort");

*/

      

    public Client() {

      

    }

   

   

    public static void main(String[] args)throws Exception {

       /**

       Service service = Service.create(SERVICE_NAME);

       String endpointAddress ="http://localhost:8080/mywbs3";

       service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,endpointAddress);

       */

      

       /**没有用spring

       URL url = newURL("http://localhost:8080/mywbs3?wsdl");

        Service service =Service.create(url,SERVICE_NAME);

      

       HelloWorld hw = service.getPort(HelloWorld.class);

       System.out.println(hw.sayHi("张述飞"));*/

      

       JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();

       svr.setServiceClass(HelloWorld.class);

       svr.setAddress("http://localhost:8080/mywbs3/HelloWorld");

       HelloWorld hw = svr.create(HelloWorld.class);   

       String result = hw.sayHi("abc");

       System.out.println("haaha=="+result);

    }

 

 

 

 

}

十一、  点击运行类Client,没有错误的话呢,控制台显示

haaha==Helloabc

 

在 tomcat控制台有可能报错,有时不报,图片没有了!

sayHi called

DefaultValidationEventHandler:[ERROR]: prefix dpp is not bound to a namespace

     Location: node: [wsd:Types: null]

javax.xml.bind.UnmarshalException: prefix dpp is not bound to a namespace

 - with linked exception:

[java.lang.IllegalArgumentException: prefix dpp is not bound to a namespace]

    atcom.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:740)

~~~~~~~~~~~

此处好长,百度了一下说什么的都有,我也没有找到具体的解决方法,好像说是jar包引用冲突了,最后,我重新根据需要导入了jar包,具体在后面(十五)有介绍。

 

 

 

十二、  使用spring,写一个类ClientWithSpring.java(第二种)

packagecom.firstws.client;

 

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

 

importcom.firstws.test.HelloWorld;

 

publicclass ClientWithSpring {

 

   

    public static void main(String[] args) {

       System.out.println("aaaaa");

       ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");

 

        HelloWorldhw = context.getBean("myhw", HelloWorld.class);

       System.out.println(hw.sayHi("张述飞 "));

      

    }

 

}

十三、  点击运行类ClientWithSpring,控制台报错

aaaaa

Exception in thread"main" java.lang.NoSuchMethodError:org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V

    atorg.apache.commons.logging.impl.SLF4JLocationAwareLog.info(SLF4JLocationAwareLog.java:159)

    atorg.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:512)

    atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:448)

    atorg.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)

    atorg.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)

    atcom.firstws.client.ClientWithSpring.main(ClientWithSpring.java:13)

问度娘,各种解答,没办法一个个试吧,最后找到一个比较靠谱的,说是slf4j包冲突。删除jcl-over-slf4j-1.7.9.jar和slf4j-jdk14-1.7.9.jar,又报错了,aaaaa

Exception in thread"main" java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory

    atorg.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:164)

    atorg.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:228)...

这个比较明显,加个commons-logging-1.1.3.jar,OK了出结果了

aaaaa

Hello 张述飞

十四、  有的博客写的用wsdl2java自动生成类(第三种,有兴趣的朋友可以试试)

这里大体讲一下,设置环境变量

 

1、CXF_HOME= E:\soap\CXF\apache-cxf-3.0.7

2、在path后面加上 %CXF_HOME%/bin;

3、也要注意JAVA_HOME的配置,我装了三个版本的JDK,一开始指向了jdk1.5结果死活不编译,还报错,将JAVA_HOME的值改为C:\Program Files\Java\jdk1.6.0_43,就正常了。apache-cxf-3.0.7最小要对应JDK1.6的。

 

在dos窗口中输入,对了还要告诉一下小白怎么样打开dos窗口,点击开始-运行-输入cmd,就可以打开了,再不会拖出去,打死100遍,呵呵!

C:\Documentsand Settings\sjb_zhangshufei>wsdl2java -v

wsdl2java- Apache CXF 3.0.7

C:\Documentsand Settings\sjb_zhangshufei>wsdl2java -d E:\MyEclipse10Workspaces\

mywbs3\src -clienthttp://localhost:8080/mywbs3/HelloWorld?wsdl

 

稍等一会儿,在myeclipse中,点击工程,右键刷新,当当当,奇迹出现了,


,多了好多东西具体怎么用就不说了,自己百度研究吧。

 

 

十五、  这个工程最简化的jar包项目,也可能多一个少一个,没有都导入!


十六、  一个小异常

这里还有个小插曲: jaxb-api 的jar包,CXF3.0依赖的是jaxb-api-2.2.11.jar,如果是jdk1.6.20以下自带的版本比较低,在运行的时候报异常,不能正常加载class,这里两个解决方案,如果不升级JDK那么将:CXF3.0.2版本中自带的endorsed, integration考到使用的JDK或者jre的lib目录下:

例如我的目录是: C:\ProgramFiles\Java\jdk1.6.0_11\jre\lib

 

 

 

第二方法,那么就是升级JDK了,这没有什么可说。

十七、  在applicationContext.xml中有的写上导入

    <importresource="classpath:META-INF/cxf/cxf.xml"/>

<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

十八、  我们从哪里找这两个文件呢,不过好像不处理也没报错,解压cxf-core-3.0.7.jar,找到E:\soap\CXF\cxf-core-3.0.7\META-INF\cxf,将整个cxf包复制到工程中就行了。还有一些别的错误没有一一写出来,学东西吗,就要一点一点的积累!工程我也上传了,有需要的下! 


下载文件

http://download.csdn.net/detail/zhangshufei8001/9384408



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值