springboot整合CXF发布webService和动态调用

不说闲话,直接附上所有源码。

1.pom文件引入jar包

         
            org.apache.cxf
            cxf-rt-frontend-jaxws
            3.1.12
        

        
            org.apache.cxf
            cxf-rt-transports-http
            3.1.12
        

2.项目目录结构

springboot整合CXF发布webService和动态调用_第1张图片

3.接口ISayHello

package com.example.cxfdemo.service;

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

/**
 * @Description TODO
 * @Author wuqingyan
 * Date 2019/5/17 10:19
 * Modify Log
 **/
@WebService(targetNamespace = "http://service.cxfdemo.example.com")
public interface IHello {

    @WebMethod
    public @WebResult String sayHello(@WebParam(name = "userName") String userName);
}

4.实现类SayHelloImpl

package com.example.cxfdemo.serviceimpl;

import com.example.cxfdemo.service.IHello;
import org.springframework.stereotype.Component;

import javax.jws.WebService;

/**
 * @Description TODO
 * @Author wuqingyan
 * Date 2019/5/17 10:22
 * Modify Log
 **/

@Component
@WebService(serviceName="helloService",  //【对外发布的服务名 】:需要见名知意
        targetNamespace="http://service.cxfdemo.example.com", //【名称空间】:要跟接口的保持一致
        endpointInterface = "com.example.cxfdemo.service.IHello") //【服务接口全路径】
public class HelloImpl implements IHello {

    @Override
    public String sayHello(String userName) {
        System.out.println("hello!"+userName);
        return "hello!"+userName;
    }
}

 5.配置类CxfConfig

package com.example.cxfdemo;

import com.example.cxfdemo.serviceimpl.HelloImpl;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
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;

import javax.xml.ws.Endpoint;

/**
 * @Description TODO
 * @Author wuqingyan
 * Date 2019/5/17 10:29
 * Modify Log
 **/
@Configuration
public class CxfConfig {

    @Autowired
    private HelloImpl hello;
    //注意这个方法名 网上好多用dispatcherServlet启动会报错
    @Bean
    public ServletRegistrationBean getDispatcherServlet() { 
        return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");// 发布服务名称 localhost:8080/cxf
    }
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    @Bean
    public Endpoint hello() {
        // 绑定要发布的服务实现类
        EndpointImpl endpoint = new EndpointImpl(springBus(),hello);
        endpoint.publish("/hello"); // 接口访问地址
        return endpoint;
    }

}

6.启动成功截图

springboot整合CXF发布webService和动态调用_第2张图片

springboot整合CXF发布webService和动态调用_第3张图片 

7.客户端调用类CxfClient

package com.example.cxfdemo.client;

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

/**
 * @Description TODO
 * @Author wuqingyan
 * Date 2019/5/17 11:09
 * Modify Log
 **/
public class CxfClient {
    public static void main(String[] args) {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://127.0.0.1:8090/cxf/hello?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("sayHello", "周杰伦");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}

8.调用成功截图

 

 以上是所有步骤和源码,欢迎参考。

你可能感兴趣的:(webservice,cxf,java笔记)