Apache CXF + SpringMVC 整合发布WebService

一、概述

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

二、实例

1.pom文件中引入cxf

 
            org.apache.cxf
            cxf-rt-frontend-jaxws
            2.6.0
        
        
            org.apache.cxf
            cxf-rt-transports-http
            2.6.0
              

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





    

    

    
    


4.添加xml到web入口


  
    contextConfigLocation
    
      classpath:config/applicationContext.xml,
      classpath:config/applicationContext-cxf.xml
    
  

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

Apache CXF + SpringMVC 整合发布WebService_第1张图片
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();
        }
    }

}

查看控制台


Apache CXF + SpringMVC 整合发布WebService_第2张图片
image.png

-end-

你可能感兴趣的:(Apache CXF + SpringMVC 整合发布WebService)