springboot整合webservice 实现项目之间的数据交互

发布时间:2023-03-07 19:30

在这之前写过一个整合webservice实现多项目之间的数据交换,现在使用RestTemplate方法实现
1.注入webservice依赖


        org.apache.cxf
        cxf-spring-boot-starter-jaxws
        3.2.5
    

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 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 findByCode(String code) {
        Query query=new Query(Criteria.where("code").is(code));
        List 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;

/**
 * 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;
    }
}

至此服务端的接口已经写好
接着我们继续写数据中心的接口。在这里数据中心要接收上边服务端发暴露的接口数据。
在写接口之前,要启动项目,查看对外暴露的接口是否发布成功

springboot整合webservice 实现项目之间的数据交互_第1张图片
出现以上内容,表示发布成功

数据交换中心代码:

package com.yankuang.datacenter.web.client;

import com.alibaba.fastjson.JSONObject;
import com.yankuang.datacenter.model.InterfaceMessage;
import com.yankuang.datacenter.service.InterfaceMessageService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    private final Logger logger = LoggerFactory.getLogger(CxfClient.class);

    @RpcConsumer
    private InterfaceMessageService interfaceMessageService;

    /**
     * 动态调用方式
     */
    @PostMapping("findByCode")
    public String findByCode(@RequestBody InterfaceMessage interfaceMessage) {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

        //从数据库中获取访问地址url
        InterfaceMessage interfaceMessage1 = interfaceMessageService.findByCode(interfaceMessage.getCode());
        String interfaceAddress = interfaceMessage1.getInterfaceAddress();//获取url
        String methodName = interfaceMessage1.getMethodName();//获取方法名
        String parameter = interfaceMessage1.getParameter();//获取参数
        //访问地址应该是没有后缀?wsdl,获取到url之后需要拼接
        String interfaceAddressWsdl = interfaceAddress + "?wsdl";
//        Client client = dcf.createClient("http://192.168.1.135:12301/services/AchieveExpressService?wsdl");
        Client client = dcf.createClient(interfaceAddressWsdl);
        String json = null;
        try {
            Object[] objects1= client.invoke(methodName, parameter);
            List object2 = (List) objects1[0];
            json = JSONObject.toJSONString(object2);
            System.out.println("返回数据:" + json.toString());

        } catch (Exception e) {
            e.printStackTrace();
            logger.info("服务器断开连接,请稍后再试");
        }
        return json;
    }
  }  
 
  

上边获取的信息,是从数据库中获取的,有前端页面可以自行配置,方便运维和用户的使用

第三方系统接口
使用 RestTemplate 调用接口

package com.yankuang.technology.web.client;


import com.yankuang.technology.model.InterfaceMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: xhh
 * @Date: 2019-01-08 14:09
 * @Version 1.0
 * 创建基于cxf的客服端调用webservice接口
 */
@RestController
@RequestMapping("/v1/cxf")
public class CxfClient {

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/findIfmByCode")
    @CrossOrigin
    public String findIfmByCode(@RequestBody InterfaceMessage i) {
        String url = "http://192.168.1.135:12307/v1/webservice/findByCode";
        InterfaceMessage u = new InterfaceMessage();
        u.setCode(i.getCode());
        String str = restTemplate.postForEntity(url, u, String.class).getBody();
        return str;
    }

}

项目启动成功后,运行接口
springboot整合webservice 实现项目之间的数据交互_第2张图片

成功

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号