springboot使用webservice发布和调用接口

springboot使用webservice发布和调用接口

加入以下依赖:

	<!-- cxf框架依赖  -->
	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
		<version>3.2.4</version>
	</dependency>

服务端service代码:

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

/**
 * TODO(描述这个类的作用) 
 * @author zyl
 * @date 2019年3月27日
 */
/**
 * 创建服务接口
 * @author oKong
 *
 */
@WebService()
public interface HelloWebService {
    @WebMethod
    public String Hello(@WebParam(name="name") String name);
}

服务端实现类代码:

import javax.jws.WebParam;
import javax.jws.WebService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TODO(描述这个类的作用) 
 * @author zyl
 * @date 2019年3月27日
 */
@WebService(
        targetNamespace = "demo.example.com", //wsdl命名空间 
        serviceName = "HelloWebService",                 //portType名称 客户端生成代码时 为接口名称
        endpointInterface = "com.example.demo.configuraction.webservice.HelloWebService")//指定发布webservcie的接口类,此类也需要接入@WebService注解
@Configuration
public class HelloWebServiceImpl implements HelloWebService{

    @Override
    public String Hello(@WebParam(name="name") String name) {
        System.out.println("欢迎你"+name);
        return "欢迎你"+name;
    }
}

服务端发布服务类:
我的端口设置为9999,所以我的服务地址为http://127.0.0.1:9090/ws/helloWebService?wsdl

import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * cxf配置类
 * @author oKong
 *
 */
@Configuration
public class CxfWebServiceConfig {
    
    @Autowired
    private Bus bus;
    @Autowired
    private HelloWebService helloWebService;
    
    @Bean("cxfServletRegistration")
    public ServletRegistrationBean dispatcherServlet() {
        //注册servlet 拦截/ws 开头的请求 不设置 默认为:/services/*
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }
    
    /*
     * 发布endpoint
     */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, helloWebService);
        endpoint.publish("/helloWebService");//发布地址
        return endpoint;
    }
}

客户端调用服务代码:
以下两种方法选一即可

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
/**
 * @ClassName:CxfClient
 * @Description:webservice客户端:
 *                 该类提供两种不同的方式来调用webservice服务
 *              1:代理工厂方式
 *              2:动态调用webservice
 * @author Jerry
 * @date:2018年4月10日下午4:14:07
 */
public class CxfClient {


    public static void main(String[] args) {
        CxfClient.main1();
//        CxfClient.main2();
    }

    /**
     * 1.代理类工厂的方式,需要拿到对方的接口地址
     */
    public static void main1() {
        try {
            // 接口地址
            String address = "http://127.0.0.1:9090/ws/helloWebService?wsdl";
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(HelloWebService.class);
            // 创建一个代理接口实现
            HelloWebService us = (HelloWebService) jaxWsProxyFactoryBean.create();
            // 数据准备
            String userId = "zz";
            // 调用代理接口的方法调用并返回结果
            String result = us.Hello(userId);
            System.out.println("返回结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 2:动态调用
     */
    public static void main2() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://127.0.0.1:9090/ws/helloWebService?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("getUserName", "maple");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}
发布了25 篇原创文章 · 获赞 58 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/student_zz/article/details/93487157
今日推荐