springboot+CXF开发webservice对外提供接口(转)

 

文章来源:http://www.leftso.com/blog/144.html 

 

1.项目要对外提供接口,用webservcie的方式实现

2.添加的jar包

maven:

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.1.7</version>
</dependency>

gradle:

compile group: 'org.apache.cxf', name: 'cxf-spring-boot-starter-jaxws', version: '3.1.7'

3.接口类

package com.webservice.test;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * Created by admin on 2017/5/9 15:02.
 */
@WebService(name = "ITestCollect", // 服务名称
        targetNamespace = "http://test.webservice.com/"// 命名空间,一般是接口的包名倒序
)
public interface ITestCollect {

    @WebMethod //@webMethod:方法注解,表示暴露的方法 @webParam:参数注解,name:参数名称  
    String test(@WebParam(name = "name") String name);
}

实现类

package com.webservice.test;

import org.springframework.stereotype.Service;

import javax.jws.WebService;

/**
 * Created by admin on 2017/5/9 15:02.
 */
@Service
@WebService(serviceName = "ITestCollect",//与接口中name一致
        targetNamespace = "http://test.webservice.com/",//与接口中的 targetNamespace 属性一致
        endpointInterface = "com.webservice.test.ITestCollect"//接口的全路径
)
public class TestCollectImpl implements ITestCollect {
    @Override
    public String test(String name) {
        System.out.println("name:" + name);
        return "success:"+name;
    }

}

 配置config

package com.webservice.test;

import com.webservice.test.ITestCollect;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * Created by admin on 2017/5/9 14:40.
 */
@Configuration
public class WebServiceConfig {
    @Autowired
    private Bus bus;

    @Autowired
    ITestCollect testCollect;

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, testCollect);
        endpoint.publish("/testCollect");
        return endpoint;
    }
}

 配置完成后 启动springboot 直接访问 http://localhost:8080/services/testCollect?wsdl

可以看到一个xml的页面,则部署成功

/services/ 是固定的,/testCollect 是代码里设置的路径  ?wsdl 是固定的

这里有一个坑:

(1)例子中的所有类都是在一个包下面的,要是在不同的包下面,会有各种错,就是找不到对应的类,可能要配置 targetNamespace 属性或者其参数,没有研究,但是放在同一个包下面就没问题,

(2)调用完成后,返回值是对象或者是list类型,方法上不能加 @WebResult 注解,不然要在对应的类上面写上实体类转xml的注解,新建的实体类也要在同一个包下,不然返回的对象找不到包路径

(3)cxf不支持重载,如果接口中有重载方法,要重命名 @WebMethod(operationName = "xxxx")  operationName属性是重新取名

4.调用

package com.webservice.test;

import com.webservice.test.ITestCollect;import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import java.util.List;

/**
 * Created by admin on 2017/5/9 20:36.
 */
public class WebServiceTest {
    public static void main(String[] args) {
        test2();
    }


    /**
     * 方式1.代理类工厂的方式,需要拿到对方的接口
     */
    public static void test1() {
        try {
            String address = "http://localhost:8080/services/testCollect?wsdl";
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            jaxWsProxyFactoryBean.setAddress(address);
            jaxWsProxyFactoryBean.setServiceClass(ITestCollect.class);
            ITestCollect cs = (ITestCollect) jaxWsProxyFactoryBean.create();
            String userName = "test";
            String result = cs.test(userName);
            System.out.println("返回结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 方式2.动态调用方式
     */
    public static void test2() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/services/testCollect?wsdl");
        Object[] objects = new Object[0];
    try { //用法:client.invoke("方法名",参数1,参数2,参数3....); // objects = client.invoke("add", model); objects = client.invoke("test"); System.out.println("success....."); System.out.println("返回数据:" + objects[0]); } catch (java.lang.Exception e) { e.printStackTrace(); } } }

文章来源:http://www.leftso.com/blog/144.html  

 

原文地址:https://www.cnblogs.com/skyessay/p/6835831.html