webservice学习

本文介绍了如何在Springboot中整合WebService,包括引入相关依赖,创建配置类生成客户端,以及调用接口的详细步骤。文章提到了WebService的组成部分和调用流程,并对比了WebService与RESTful接口的区别。
摘要由CSDN通过智能技术生成

WebService

Web Service也称为web服务,它是一种跨编程语言和操作系统平台的远程调用技术。Web Service采用标准的SOAP协议传输(SOAP:Simple Object Access Protocol简单对象访问协议,soap属于w3c标准。并且soap协议是基于http的应用层协议传输xml数据)。Web Service采用WSDL作为描述语言,也就是Web Service 的使用说明书。并且W3C为Web Service制定了一套传输数据类型,使用xml进行描述,即XSD(XML Schema Datatypes),任何语言写的web Service 接口在发送数据的时候都要转换成WebService标准的XSD发送。

Springboot整合WebService

webservice简介:

在这里插入图片描述

webservice项目分为三个模块:各个组件(common)、服务端(server)、客户端(client)

客户端调用流程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VycGbpld-1690688136675)(C:\Users\lixuanlu\Desktop\111\实习收获\image-20230718111209412.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QJRbei36-1690688136675)(C:\Users\lixuanlu\Desktop\111\实习收获\image-20230718111317921.png)]

使用步骤

可以这样理解,以前的系统通过 Web Services 对接数据。现在的系统使用 RESTful 接口 + JSON数据格式 对接数据,技术在不断的更新迭代,但是使用技术的目的从未改变,那就是对接数据。

1.引入依赖

<!-- webService-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.1</version>
        </dependency>

2.创建配置类,根据接口路径生成Client 交给Spring 管理

@Confjguration
@slf4j
public class JaxWsClientConfig {
	@Bean("jaxWsClient")
	public Client client(){
		// 创建动态客户端
		JaxWsDynamicClientFactory clientFactory= JaxWsDynamicClientFactory.newInstance();
        log.info("public security webService url : ""http://10,189,98,170/ws/info?wsdl");//创建cLient
		Client client = clientFactory.createClient( wsdlUr: "http://10,189.98.170/ws/info?wsdl");
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
		HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setAllowChunking(false):
        //连接服务器超时时间 10秒
        policy.setConnectionTimeout(10000):
        //等待服务器响应超时时间 20秒
        policy.setReceiveTimeout(20000):
        conduit.setClient(policy);
        return client:
    }
}

3、调用接口

@slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService {
    @Autowired
    @Qualifier("JaxWsClient")
    private Client client;
   
        @Override
        public String getInfo(User user) {
        try {
            // 下面一段处理 WebService接口和实现类namespace不同的情况
            // CXF动态客户端在处理此问题时,会No operation was found with the name的异常
            Endpoint endpoint = client.getEndpoint();
            OName opName = new ONameendpoint,getService().getName() .getNamespaceuRI(), localPart: "query") ;
            log.info("opName:{}" ,opName);
            BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding();
            if (bindingInfo.getOperation(opName) == nu11) {
                for (BindingOperationInfo operationInfo : bindingInfo,getOperations()) {
                    if ("query".equals(operationInfo.getName().getLocalpart())) {
                        opName = operationInfo.getName();
                    break;
                    }
                }
            }
            //对user 变量进行赋值,并实现序列化
            user.setId("ce2"):
            user.setApplySys("cTs") ;
            ObjectMapper objectMapper = new ObjectMapper();
            String userJson = objectMapper.writeValueAsString(user);
            Object[] invoke = client.invoke(opName,userJson); //调用接口
            if (invoke != null && invoke.length >= 1) {
                String result = JSON.toJSONString(invoke[0],SerializerFeature.WriteslashAsSpecial);
                log.info("userAddNew result:{}",result);
                return result;
            }
        }catch (Exception e){
        log.error("invoke Ws userAddNew method error: (1", e.getMessage());
        }
        return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值