https://www.jianshu.com/p/527d3ba6ad14

 

一、概述

webservice其实是个类似文档的东西,保证不同平台应用之间可以相互操作,之前都是使用java拓展包进行ws发布,生成一个代理jar进行调用,这次试试springmvc+cxf,不过这个还是soap方式的,restful方式的需要研究一下jersy再放出来。

二、实例

1.pom文件中引入cxf

 <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>2.6.0</version>
        </dependency>

2.webservice 接口 及实现类

package com.web.cxf.service;

import javax.jws.WebService;

@WebService
public interface CXFWebService {

    public String hello(String text);

}
package com.web.cxf.service.impl;

import com.web.cxf.service.CXFWebService;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(endpointInterface = "com.web.cxf.service.CXFWebService",
        targetNamespace = "http://service.cxf.web.com/",
        serviceName = "CXFWebService")
public class CXFWebServiceImpl implements CXFWebService{

    public String hello(@WebParam String text) {
        return "Hello " + text;
    }
}

3.配置applicationContext-cxf.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:amq="http://activemq.apache.org/schema/core"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xmlns:mvc="http://www.alibaba.com/schema/stat"
       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://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.7.0.xsd
        http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd">


    <context:annotation-config />

    <bean id="test" class="com.web.cxf.service.impl.CXFWebServiceImpl" />

    <!--   implementor 是bean中的id;address是暴漏出来的地址。-->
    <jaxws:endpoint id="testCXF" implementor="#test" address="/testCXF" />

</beans>

4.添加xml到web入口

<!-- 1.针对Spring配置:读取配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:config/applicationContext.xml,
      classpath:config/applicationContext-cxf.xml
    </param-value>
  </context-param>

5.启动项目查看http://localhost:8080/cxf/testCXF?wsdl

 
image.png

6.客户端调用

package com.web.cxf.client;


import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import javax.xml.namespace.QName;

public class CXFWebServiceClient {

    public static void main(String[] args) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/cxf/testCXF?wsdl");
        Object[] objects;
        //使用qname 解决该命名空间下找不到方法的问题
//        //QName qname;
//        try {
//            qname = new QName("http://service.cxf.web.com/","hello");
//            objects = client.invoke(qname,"AnyFast");
//            System.out.println(objects[0].toString());
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        try {
            objects = client.invoke("hello","AnyFast");
            System.out.println(objects[0].toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

查看控制台


 
image.png

-end-



作者:榆次丶笑笑生
链接:https://www.jianshu.com/p/527d3ba6ad14
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
12-24 09:28