springboot 整合webservice 多项目跨系统相互调用接口

springboot 整合webservice 多项目跨系统相互调用接口
最近需要做一个关于webservice作为中间数据处理的系统,需要调用相关系统的数据,然后将数据处理好之后,暴露给第三方系统。
1.整合webservice
webservice 注入依赖在这里插入我图片描述

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

2.服务端用webservice暴露服务接口

import com.yankuan.model.AchieveExpress;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.util.List;

@WebService(name = “AchievementService”, // 暴露服务名称
targetNamespace = “http://service.web.yankuan.com”) //命名空间,一般是接口的包名倒序
public interface AchieveExpressService {

@WebMethod
@WebResult(name = "String",targetNamespace = "")
List<AchieveExpress> findByCode(@WebParam(name = "code") String code);

}

实现类

import com.yankuan.model.AchieveExpress;
import com.yankuan.web.service.AchieveExpressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

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

@WebService(serviceName = “AchieveExpressService”,//与前面接口一致
targetNamespace = “http://service.web.yankuan.com”, //与前面接口一致
endpointInterface = “com.yankuan.web.service.AchieveExpressService”) //接口地址
@Component
public class AchieveExpressServiceImpl implements AchieveExpressService {

@Autowired
MongoTemplate mongoTemplate;

@Override
public List<AchieveExpress> findByCode(String code) {
    Query query=new Query(Criteria.where("code").is(code));
    List<AchieveExpress> dyns= mongoTemplate.find(query,AchieveExpress.class);
    return dyns;
}

}
3.服务端发布相关的接口(根据自己的实际情况可发布多个)

import com.yankuan.web.service.AchieveExpressService;
import com.yankuan.web.service.HelloService;
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;

/**

  • @Author: xhh

  • @Date: 2019-01-08 13:58

  • @Version 1.0

  • webservice
    */
    @Configuration
    public class WebConfig {

    @Autowired
    private Bus bus;

    @Autowired
    HelloService helloService;

    @Autowired
    AchieveExpressService achieveExpressService;

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

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

}

至此服务端的接口已经写好
接着我们继续写数据中心的接口。在这里数据中心要接收上边服务端发暴露的接口数据。
这里我们将数据中心也看作是一个服务端,将接收到的数据,通过相应的业务处理之后,也用上边同样的方式进行接口的暴露,最后将数据提供个第三方系统。
数据中心接口
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;

/**

  • @Author: xhh

  • @Date: 2019-01-16 16:53

  • @Version 1.0
    */
    @WebService(name = “CxfClientService”, // 暴露服务名称
    targetNamespace = “http://service.datacenter.yankuan.com”) //命名空间,一般是接口的包名倒序
    public interface CxfClientService {

    @WebMethod
    @WebResult(name = “String”,targetNamespace = “”)
    Object findByCode();

}
实现类
代码中的接口http://192.168.1.135:12301/services/AchieveExpressService?wsdl 需要提供,还有接口里相应的方法以及传的参数
import com.alibaba.fastjson.JSONObject;
import com.yankuan.datacenter.service.CxfClientService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;

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

@WebService(serviceName = “CxfClientService”,//与前面接口一致
targetNamespace = “http://service.datacenter.yankuan.com”, //与前面接口一致
endpointInterface = “com.yankuan.datacenter.service.CxfClientService”) //接口地址
@Component
@RpcProvider
public class CxfClientServiceImpl implements CxfClientService {

/**
 * 动态调用方式
 */

public String findByCode() {
    // 创建动态客户端
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient("http://192.168.1.135:12301/services/AchieveExpressService?wsdl");
    String json = null;
    try {
        // invoke("方法名",参数1,参数2,参数3....);
        Object[] objects1= client.invoke("findByCode", "1");
        List<Object> object2 = (List<Object>) objects1[0];
        json = JSONObject.toJSONString(object2);
        System.out.println("返回数据1111111111:" + json.toString());

    } catch (Exception e) {
        e.printStackTrace();
    }

    return json;
}

发布接口:

import com.yankuan.datacenter.service.CxfClientService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
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;

/**

  • @Author: xhh

  • @Date: 2019-01-08 13:58

  • @Version 1.0

  • webservice
    */
    @Configuration
    public class WebConfig {

    @Autowired
    private Bus bus;

    @Autowired
    CxfClientService cxfClientService;

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

}

第三方系统调用:

import com.alibaba.fastjson.JSONObject;
import com.yankuan.model.AchieveExpress;
import com.yankuan.web.service.AchieveExpressService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**

  • @Date:

  • @Version 1.0

  • 创建基于cxf的客服端调用webservice接口
    */
    @RestController
    @RequestMapping("/v1/tech/")
    public class CxfClient {

    /**

    • 方式1.代理类工厂的方式,需要拿到对方的接口
      */
      public static void cl1() {
      try {
      // 接口地址
      String address = “http://localhost:12301/services/AchieveExpressService?wsdl”;
      // 代理工厂
      JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
      // 设置代理地址
      jaxWsProxyFactoryBean.setAddress(address);
      // 设置接口类型
      jaxWsProxyFactoryBean.setServiceClass(AchieveExpress.class);
      // 创建一个代理接口实现
      AchieveExpressService cs = (AchieveExpressService) jaxWsProxyFactoryBean.create();
      // 调用代理接口的方法调用并返回结果
      List byCode = cs.findByCode(id);
      System.out.println(“返回结果:” + byCode);
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

    /**

    • 动态调用方式
      */
      @GetMapping(“findByCode”)
      public Object findByCode() {
      // 创建动态客户端
      JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
      Client client = dcf.createClient(“http://192.168.1.135:12312/services/CxfClientService?wsdl”);
      String objects = “”;
      try {
      objects = (String) client.invoke(“findByCode”)[0];
      System.out.println(“返回数据:” + objects);
      } catch (Exception e) {
      e.printStackTrace();
      }
      return objects;
      }

}

postman 测试
成功

}

猜你喜欢

转载自blog.csdn.net/qq_37833810/article/details/86518742