zoukankan      html  css  js  c++  java
  • [置顶] 利用CXF发布webService的小demo

    其实webService的发布不仅仅只有xfire,今天,给大家介绍一下用CXF发布一个webService的小demo,CXF也是我做webService用的第一个框架。。。
    先将相关的jar引进来,在pom文件中添加
    <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-transports-http</artifactId>
          <version>2.7.3</version>
      </dependency>
      
      <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-rt-frontend-jaxws</artifactId>
       <version>2.7.3</version>
      </dependency>
    即可引入相关的
    第一步,不废话,还是写一个简单的接口与实现类,代码如下
    public interface SayCXFService {
    public  String sayHello(String name);
    }
    public class SayCXFServiceImpl implements SayCXFService{
    @Override
    public String sayHello(String name) {
      return name +" hello,this is my first CXF webService!";
    }
    }
    接下来就是重点了,配置文件了,配置文件如下。相应的解释也有
    <?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-3.0.xsd
                            http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"
    default-lazy-init="true"
    >
      <!-- cxf 相关配置,这个照搬就行了,cxf框架里面自带的配置 -->
    <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" />

    <!-- service定义 -->
    <bean id="sayCXFbean"
      class="com.mip.biz.webservice.cxf.server.service.impl.SayCXFServiceImpl"
      scope="singleton"
    />
    <!--
    serviceClass 表示你要对外开发的接口,address表示的地址,本质就是一个servletName,可以随便命名
       -->
    <jaxws:server id="webService" serviceClass="com.mip.biz.webservice.cxf.server.service.SayCXFService" address="/server">
         <!-- 表示你的接口实现类的bean -->
      <jaxws:serviceBean>
       <ref bean="sayCXFbean"/>
      </jaxws:serviceBean>
    </jaxws:server>
    </beans>

    然后是web.xml文件添加如下内容
    <servlet>
      <description>apache cxf 配置 webservice 服务</description>
      <servlet-name>CXFService</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>CXFService</servlet-name>
      <url-pattern>/CXFService/*</url-pattern>
    </servlet-mapping>
    除了测试类,似乎和前段时间的xfire没啥区别,试一下启动tomcat,会发现抱NullPointerException
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webService': Invocation of init method failed; nested exception is java.lang.NullPointerException
    别急,你只需要在接口上稍微改动一下即可

    //注解的方式,将接口注入,targetNamespace中的内容其实就是将包名写反就行了。。这也是我们平时包名的命名规则类似。com开头,然后模块名称..有么像www.ho123.com?
    @WebService(targetNamespace = "
    http://service.server.cxf.webservice.biz.mip.com/")
    public interface SayCXFService {
    public  String sayHello(String name);
    }
    //实现类
    @WebService(endpointInterface="com.mip.biz.webservice.cxf.server.service.SayCXFService",
      targetNamespace = "
    http://service.server.cxf.webservice.biz.mip.com/")
    public class SayCXFServiceImpl implements SayCXFService{
    @Override
    public String sayHello(String name) {
      return name +" hello,this is my first CXF webService!";
    }
    }
    好了重启不会报错了
    然后在浏览器中输入
    http://localhost:8080/mip/CXFService/server?wsdl出现祖国江山一片红的景象就ok了
    - <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.server.cxf.webservice.biz.mip.com/" elementFormDefault="unqualified" targetNamespace="http://service.server.cxf.webservice.biz.mip.com/" version="1.0">
    <xs:element name="sayHello" type="tns:sayHello" />

    <xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />

    - <xs:complexType name="sayHello">
    + <xs:sequence>
    <xs:element minOccurs="0" name="arg0" type="xs:string" />

    </xs:sequence>


    </xs:complexType>


    - 。。。。。。
    注意到上面的红色标记部分没,其实我们可以将参数的名字显示出来,改下接口
    @WebService(targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/")
    public interface SayCXFService {
    @WebMethod
    @WebResult
    //下面的name="wsdl中暴露出来的接口参数名称"
    public  String sayHello(@WebParam(name="name",targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/") String name);
    }
    重新看下wsdl文件:




    - <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.server.cxf.webservice.biz.mip.com/" elementFormDefault="unqualified" targetNamespace="http://service.server.cxf.webservice.biz.mip.com/" version="1.0">
    <xs:element name="sayHello" type="tns:sayHello" />

    <xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />

    - <xs:complexType name="sayHello">
    - <xs:sequence>
    <xs:element form="qualified" minOccurs="0" name="name" type="xs:string" />

    </xs:sequence>


    </xs:complexType>


    -

    以上是用java开发的服务端,下一章我们接着写客户端与测试类
    @WebService(targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/")
    public interface SayCXFClient {
    public  String sayHello(String name); }
    客户端其实直接将服务端拷贝过来就可以了。。。。当然这是在java开发的服务端基础上你才可以做,假如你的webservice服务端是其他语言开发的,,,那么可以用其他方法生成客户端咯,主要依据的是wsdl文件。。这个我在下一篇博客给大家介绍。。。请期待
    最后在写个测试类
    public class Test {
    public static void main(String[] args) {
      JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
      factory.setServiceClass(SayCXFClient.class);
      factory.setAddress("http://localhost:8080/mip/CXFService/server");
      SayCXFClient service = (SayCXFClient) factory.create();
           System.out.println(service.sayHello("zhouxy"));
    }
    }
    运行输出
    Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Unmarshalling Error: unexpected element (uri:"", local:"arg0"). Expected elements are <{http://service.server.cxf.webservice.biz.mip.com/}name>
    这个是由于我们在服务端明确指出了参数名name照成的,别担心,改下就行了
    @WebService(
    targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/")
    public interface SayCXFClient {
    @WebMethod
    @WebResult
    //下面的name="wsdl中暴露出来的接口参数名称"
    public  String sayHello(@WebParam(name="name",targetNamespace = "http://service.server.cxf.webservice.biz.mip.com/") String name);
    }
    继续运行输出:
    zhouxy hello,this is my first CXF webService!
    ok,demo结束,更详细的理论知识自己查资料去吧

  • 相关阅读:
    【做题】agc003E
    步态识别问题简介
    win10 +Kinect V1 1414环境配置
    生物特征识别数据泄露事件
    人脸识别应用领域
    爱自然
    wxid 转微信号
    Blahut-Arimoto algorithm Matlab源码
    Theorem、Proposition、Lemma和Corollary等的解释与区别
    PokerNet-poker recognition: 扑克识别 (6)
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3241625.html
Copyright © 2011-2022 走看看