SpringBoot整合cxf发布WebService服务和客户端调用WebService服务

一、依赖关系

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ming</groupId>
    <artifactId>ming-web-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ming-web-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.1.11</version>
        </dependency>
        <!-- CXF webservice -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二、服务端接口

1、接口

package com.ming.webservice;

import javax.jws.WebMethod;
import javax.jws.WebService;
import java.util.List;

/**
 * 版权:    Copyright by ljm
 * 描述:
 * 修改人:  HuamingChen
 * 修改时间:2019/5/10
 * 跟踪单号:
 * 修改单号:
 * 修改内容:
 */
@WebService(name="testService",
targetNamespace = "http://webservice.ming.com")
public interface TestService {
    @WebMethod
    List<User> sendMsg(String userName, int age);
}

2、实现

package com.ming.webservice;

import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;

/**
 * 版权:    Copyright by ljm
 * 描述:
 * 修改人:  HuamingChen
 * 修改时间:2019/5/10
 * 跟踪单号:
 * 修改单号:
 * 修改内容:
 */
@WebService(name="testService",
        targetNamespace = "http://webservice.ming.com",
        endpointInterface = "com.ming.webservice.TestService")
@Component
public class TestServiceImpl implements TestService {
    @Override
    public List<User> sendMsg(String userName, int age) {
        List<User> list=new ArrayList<>();
        for(int i=0;i<10;i++){
            User user=new User();
            user.setUserName("ming"+i);
            user.setAge(i);
            list.add(user);
        }
        return list;
    }
}

3、cxf配置

package com.ming.webservice;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;

/**
 * 版权:    Copyright by ljm
 * 描述:
 * 修改人:  HuamingChen
 * 修改时间:2019/5/10
 * 跟踪单号:
 * 修改单号:
 * 修改内容:
 */
@Configuration
public class CxfConfig {
    @Resource
    private Bus bus;

    @Resource
    private TestService testService;

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

4、实体

package com.ming.webservice;

/**
 * 版权:    Copyright by ljm
 * 描述:
 * 修改人:  HuamingChen
 * 修改时间:2019/5/10
 * 跟踪单号:
 * 修改单号:
 * 修改内容:
 */
public class User {
    private String userName;
    private int age;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

默认服务在Host:port/services/*路径下
将TestService接口发布在了路径/services/testService下,wsdl文档路径为,http://localhost:8080/services/testService?wsdl

三、客户端

package com.ming.webservice;

/**
 * 版权:    Copyright by ljm
 * 描述:
 * 修改人:  HuamingChen
 * 修改时间:2019/5/10
 * 跟踪单号:
 * 修改单号:
 * 修改内容:
 */

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * webservice客户端
 *
 *
 * @author:WangYuanJun
 * @date:2017年12月19日 下午9:39:49
 */
public class WebServiceTest {

    public static void main(String[] str){

        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/services/testService?wsdl");

        // 需要密码的情况需要加上用户名和密码
        client.getOutInterceptors().add(new ClientLoginInterceptor("ming","123456"));
        // 输入报文拦截器
        client.getInInterceptors().add(new LoggingInInterceptor());
        // 输出报文拦截器
        client.getOutInterceptors().add(new LoggingOutInterceptor());
        Object[] objects = new Object[0];
        try {

            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("sendMsg", "ming",20);
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}

package com.ming.webservice;

/**
 * 版权:    Copyright by ljm
 * 描述:
 * 修改人:  HuamingChen
 * 修改时间:2019/5/10
 * 跟踪单号:
 * 修改单号:
 * 修改内容:
 */
import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * 类说明
 * @author wangjunyu
 * @createDate 2016-10-20 下午8:53:16
 * @version V1.0
 */
public class ClientLoginInterceptor extends AbstractPhaseInterceptor<SoapMessage>
{

    public ClientLoginInterceptor(String username, String password)
    {
        super(Phase.PREPARE_SEND);
        this.username = username;
        this.password = password;
    }


    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public void handleMessage(SoapMessage soap) throws Fault
    {
        List<Header> headers = soap.getHeaders();
        Document doc = DOMUtils.createDocument();
        Element auth = doc.createElement("authrity");
        Element username = doc.createElement("username");
        Element password = doc.createElement("password");

        username.setTextContent(this.username);
        password.setTextContent(this.password);

        auth.appendChild(username);
        auth.appendChild(password);

        headers.add(0, new Header(new QName("tiamaes"),auth));
        System.out.println(doc);
    }

}

这个是个很简单的例子,是我项目使用到简单测试一下。至于比较深入的,请各位不吝赐教。
提供相关资料也好,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值