SpringBoot 整合 WebService

服务端

pom.xml依赖中添加

        <!-- CXF webservice 服务端 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>

application.yml 配置文件

根据实际情况选择注释掉 Eureka 配置

server:
  port: 9001

spring:
  application:
    name: warehouse-map

# Eureka 服务注册中心
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:6868/eureka/
  instance:
    prefer-ip-address: true #服务之间可以相互调用

 

SpringBoot启动类

可以根据实际情况选择注释掉 Eureka 的相关注解

package com.puzek.map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient //Eureka 客户端,将服务注册到 Eureka 服务注册中心
@EnableDiscoveryClient //发现服务
@EnableFeignClients //发现服务
@ServletComponentScan
public class MapApplication {

    public static void main(String[] args) {
        SpringApplication.run(MapApplication.class, args);
    }

}
WebService接口
package com.puzek.map.test;

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

/**
 * WebService 测试接口
 * Created by 叔公 on 2019-07-31.
 */
@WebService(name = "TestWebService", targetNamespace = "http://test.map.puzek.com")
public interface ITestWebService<T> {

    @WebMethod
    @WebResult(name = "String", targetNamespace = "")
    String sendMessage(@WebParam(name = "username") String username);

    @WebMethod
    List<T> sendList(@WebParam(name = "list") List<T> list);

}
WebService 接口实现类
package com.puzek.map.test;

import org.springframework.stereotype.Component;

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

/**
 * WebService 测试接口实现类
 * <p>
 * wsdl文档路径: http://localhost:9001/services/TestWebService?wsdl
 * <p>
 * Created by 叔公 on 2019-07-31.
 */
// serviceName:与接口中指定的name一致,  targetNamespace:与接口中的命名空间一致,一般是接口的包名倒, endpointInterface:接口地址
@WebService(serviceName = "TestWebService", targetNamespace = "http://test.map.puzek.com", endpointInterface = "com.puzek.map.test.ITestWebService")
@Component
public class TestWebServiceImpl implements ITestWebService {

    @Override
    public String sendMessage(String username) {
        return "来了..." + username;
    }

    @Override
    public List<String> sendList(List list) {
        return list;
    }
}
CXF 配置类
package com.puzek.map.test;

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;

/**
 * CXF 配置
 * Created by 叔公 on 2019-07-31.
 */
@Configuration
public class CxfConfig {

    @Autowired
    private Bus bus;

    @Autowired
    private ITestWebService testWebService;

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

服务端编写完成,直接运行 SpringBoot 启动类即可完成服务发布。

客户端

新建一个项目

pom.xml 中添加依赖

        <!-- CXF webservice 客户端-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>

编写测试方法

package com.puzek.task;

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

import java.util.ArrayList;
import java.util.List;

/**
 * WebService 客户端测试
 * <p>
 * Created by 叔公 on 2019-07-31.
 */
public class TestWebServiceClient {

    @Test
    public void test1() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:9001/services/TestWebService?wsdl");

        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects;
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("sendMessage", "刘德华");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:9001/services/TestWebService?wsdl");

        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        try {
            List<String> list = new ArrayList<>();
            list.add("刘德华");
            list.add("张学友");
            list.add("郭富城");

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

}

运行测试方法........

......
......
......
18:16:54.747 [main] DEBUG org.apache.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on interceptor org.apache.cxf.jaxws.interceptors.SwAInInterceptor@10ad20cb
18:16:54.747 [main] DEBUG org.apache.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on interceptor org.apache.cxf.jaxws.interceptors.HolderInInterceptor@26d10f2e
18:16:54.747 [main] DEBUG org.apache.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on interceptor org.apache.cxf.ws.policy.PolicyVerificationInInterceptor@c20be82
18:16:54.748 [main] DEBUG org.apache.cxf.ws.policy.PolicyVerificationInInterceptor - Verified policies for inbound message.
返回数据:[刘德华, 张学友, 郭富城]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值