发布时间: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;
}
}
至此服务端的接口已经写好
接着我们继续写数据中心的接口。在这里数据中心要接收上边服务端发暴露的接口数据。
在写接口之前,要启动项目,查看对外暴露的接口是否发布成功
数据交换中心代码:
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
上边获取的信息,是从数据库中获取的,有前端页面可以自行配置,方便运维和用户的使用
第三方系统接口
使用 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;
}
}
成功
【云原生Kubernetes系列第五篇】kubeadm v1.20 部署K8S 集群架构(人生这道选择题,总会有遗憾)
【SpringCloud】10-Nacos服务注册与配置中心
吴恩达机器学习作业-Linear Regression(Python实现)
Unity 工具类 之 Blender 的下载和简单使用(为捏脸做准备)
【Xilinx Vivado时序分析/约束系列5】FPGA开发时序分析/约束-IO时序分析
华为云Stack首席架构师:打造“称手”的数字化工具,答好政企IT数字化转型这道必选题
这些年遇到的RocketMQ消息消费超时/消费异常重试机制导致的重复消费问题(并发消费和顺序消费)源码分析
Knowledge structure enhanced graph representation learning model for attentive knowledge tracing