iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >【三】SpringBoot开发WebService接口
  • 628
分享到

【三】SpringBoot开发WebService接口

springbootjava 2023-09-04 06:09:20 628人浏览 安东尼
摘要

SpringBoot开发WEBService接口 一、服务端开发 1、引入依赖 org.springframework.boot

SpringBoot开发WEBService接口

一、服务端开发

1、引入依赖

                <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-web-servicesartifactId>        dependency>        <dependency>            <groupId>org.apache.cxfgroupId>            <artifactId>cxf-rt-frontend-jaxwsartifactId>            <version>3.2.0version>        dependency>        <dependency>            <groupId>org.apache.cxfgroupId>            <artifactId>cxf-rt-transports-HttpartifactId>            <version>3.2.0version>        dependency>        <dependency>            <groupId>org.apache.cxfgroupId>            <artifactId>cxf-coreartifactId>            <version>3.3.5version>        dependency>        <dependency>            <groupId>org.apache.cxfgroupId>            <artifactId>cxf-rt-transports-httpartifactId>            <version>3.2.4version>        dependency>

2、创建接口BlogService

package com.webserver.myb.service;import org.springframework.stereotype.Component;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;@WebService(name = "MybBlog",  // 与接口中指定的name一致targetNamespace = "http://blogService.service.myb.blog.com", // 与接口中的命名空间一致,一般是接口的包名倒)public interface BlogService {    @WebMethod    public String send(@WebParam(name = "username") String username);    @WebMethod    public String message(@WebParam(name = "message") String message);}

3、实现BlogService 接口

package com.webserver.myb.service.imppl;import com.webserver.myb.service.BlogService;import org.springframework.stereotype.Component;import javax.jws.WebService;@Component@WebService(name = "MybBlog",  // 与接口中指定的name一致targetNamespace = "http://blogService.service.myb.blog.com", // 与接口中的命名空间一致,一般是接口的包名倒        endpointInterface = "com.webserver.myb.service.BlogService"// 接口地址)public class BlogServiceImpl implements BlogService {    @Override    public String send(String username) {        if ("zhangsan".equals(username)) {            return "张三";        }        return "李四,王五";    }    @Override    public String message(String message) {        return "====Hello ====WebServer===" + message;    }}

4、创建WebService配置文件

package com.webserver.myb.config;import com.webserver.myb.service.BlogService;import com.webserver.myb.service.imppl.BlogServiceImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.apache.cxf.Bus;import org.apache.cxf.bus.spring.SpringBus;import org.apache.cxf.jaxws.EndpointImpl;import org.apache.cxf.transport.servlet.CXFServlet;import org.springframework.boot.web.servlet.ServletReGIStrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.xml.ws.Endpoint;@Configurationpublic class CxfConfig {    @Autowired    private BlogService blogService;    @Bean    public ServletRegistrationBean disServlet() {        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/webServicepublic class ClientUtil {    public static String callWebSV(String url, String methodName, String... params) throws Exception {        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();        Client client = dcf.createClient(url);        Object[] objects;        // invoke("方法名",参数1,参数2,参数3....);        objects = client.invoke(methodName, params);        return objects[0].toString();    }}

测试方法

    public static void main(String[] args) {        System.out.println("======开始调用webservice接口=====");        String url = "http://localhost:8080/webService/ws/api?wsdl";        String methodName = "send";        String result="";        try {            result= ClientUtil.callWebSV(url, methodName, "lisi");        } catch (Exception e) {            e.printStackTrace();        }        System.out.println("结果集为---" + result);    }

三、说明

1、对接口的wsdl文档解析

一个WSDL文档通常包含8个重要的元素,即definitions、types、import、message、portType、operation、binding、service元素。这些元素嵌套在definitions元素中,definitions是WSDL文档的根元素。

WSDL 服务进行交互的基本元素: Types(消息类型):数据类型定义的容器,它使用某种类型系统(如 XSD)。
Message(消息):通信数据的抽象类型化定义,它由一个或者多个 part 组成。 Part:消息参数
PortType(端口类型):特定端口类型的具体协议和数据格式规范。,它由一个或者多个 Operation组成。
Operation(操作):对服务所支持的操作进行抽象描述,WSDL定义了四种操作:
1.单向(one-way):端点接受信息;
3.要求-响应(solicit-response):端点发送消息,然后接受相关消息;
4.通知(notification[2] ):端点发送消息。

Binding:特定端口类型的具体协议和数据格式规范。
Port:定义为绑定和网络地址组合的单个端点。
Service:相关端口的集合,包括其关联的接口、操作、消息等。
外层结构里面也可能有多层结构。

2、Types

types元素作为一个容器,用于定义xml模式内置类型中没有描述的各种数据类型。当声明消息部分的有效时,消息定义使用了types元素中定义的数据类型和元素。

例如上面的例子:

    该部分定义了两个元素,send和sendResponse    send:定义了一个复杂类型,用于操作的参数传入。并指明了参数的数据类型。    sendResponse:定义了一个复杂类型,包含了一个简单的字符串,用于描述操作的返回值,并指明返回结果数据类型。

WSDL文档解析types标签

3、import

import元素可以在当前的WSDL文档中使用其他WSDL文档中指定的命名空间中的元素。
通常用于模块化WSDL文档。
必须有namespace属性和location属性:
1.namespace属性:值必须与正导入的WSDL文档中生命的targetNameSpace相匹配。
2.location属性:必须执行一个实际的WSDL文档,并且该文档不能为空

4、message

message元素描述了Web服务使用消息的有效负载。message元素可以描述输出或者接受消息的有效负载。还可以描述SOAP文件头和错误detail元素的内容。定义message元素的方式取决于使用rpc样式还是文档样式的消息传递。
在文本中的message元素的定义,本文档使用了采用文档样式的消息传递:

wsdl文档解析message

5、portType

portType元素定义了Web服务的抽象接口。该接口有点类似Java的接口,都是定义了一个抽象类型和方法,没有定义实现。在WSDL中,portType元素是由binding和service元素来实现的,这两个元素用来说明Web服务实现使用的Internet协议、编码方案、以及Internet地址。

6、binding

binding元素将portType映射到一组具体协议(SOAP和HTTP),消息传递样式、编码样式。通常binding元素与协议转悠的元素和在一起使用。

7、service 和 port

service元素包含一个或者多个port元素,其中每个port元素表示一个不同的Web服务。port元素将URL赋给一个特定的binding,甚至可以使两个或者多个port元素将不同的URL赋值给相同的binding。

来源地址:https://blog.csdn.net/weixin_56106195/article/details/128777209

--结束END--

本文标题: 【三】SpringBoot开发WebService接口

本文链接: https://www.lsjlt.com/news/393079.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 【三】SpringBoot开发WebService接口
    SpringBoot开发WebService接口 一、服务端开发 1、引入依赖 org.springframework.boot ...
    99+
    2023-09-04
    spring boot java
  • MyEclipse开发一个webservice接口
    一直以来对于接口这个东西都很好奇,各种客户也一直在说那个什么什么数据我们提供给你们一个接口就好了,结果还是不是很明白.于是乎就有了下面的小故事,接下来我们就进入正文吧---大概可以分为这样的步骤新建webservice project,Fr...
    99+
    2023-05-31
    myeclipse webservice 接口
  • java怎么实现webservice接口开发
    要实现Java的Web Service接口开发,可以使用Java的一些框架和库来帮助开发。以下是一个基本的步骤示例:1. 创建一个J...
    99+
    2023-09-15
    java webservice
  • Eclipse发布WebService和调用WebService接口
    要发布WebService,您需要遵循以下步骤:1. 创建一个Java项目,并将其导入到Eclipse中。2. 在项目中创建一个类,...
    99+
    2023-09-08
    Eclipse
  • SpringBoot调用第三方WebService接口的两种实现方式
    WebService简介 WebService接口的发布通常一般都是使用WSDL(web service descriptive language)文件的样式来发布的,该文档包含了请求的参数信息,返回...
    99+
    2023-09-12
    spring boot java 后端
  • SpringBoot如何使用axis调用webservice接口
    本篇内容介绍了“SpringBoot如何使用axis调用webservice接口”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!WebServ...
    99+
    2023-07-02
  • SpringBoot调用第三方WebService接口的操作技巧(.wsdl与.asmx类型)
    SpringBoot调webservice接口,一般都会给你url如: http://10.189.200.170:9201/wharfWebService/services/Wha...
    99+
    2024-04-02
  • Python调用webservice接口
    from suds.client import Client url = "http://218.90.174.179:50038/JmYfjzService/clientRegisterServiceWSDL" test = Clie...
    99+
    2023-01-31
    接口 Python webservice
  • Python访问WebService接口
    1.使用Python访问WebService远程方法 1)安装Sud库 https://fedorahosted.org/suds/ 下载 tarball 2)示例代码如下: from suds.client import Client...
    99+
    2023-01-31
    接口 Python WebService
  • python在webservice接口测
        接口测试第二波,webservice接口来咯,欢迎各位小伙伴吐槽~    本次拿免费的互联网国内手机号码归属地查询WEB服务webservice接口做例子,当然有很多免费webservice接口可以供大家使用,百度一下就有N多,手机...
    99+
    2023-01-31
    接口 python webservice
  • SpringBoot接口开发教程(httpclient客户端)
    目录SpringBoot接口开发服务端客户端post请求get请求SpringBoot之httpclient使用引入相关依赖编写相关工具类业务代码中使用SpringBoot接口开发 ...
    99+
    2024-04-02
  • 如何调用webservice接口
    如何调用webservice接口,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。调用方法:首先选中“项目”选项卡,点击鼠标右键,在弹出的右键菜单中选择“new”-“other...
    99+
    2023-06-20
  • webservice接口怎么调用
    调用WebService接口有多种方法,下面以Java语言为例进行说明: 使用JAX-WS(Java API for XML We...
    99+
    2024-02-29
    webservice
  • webservice接口如何调用
    调用webservice接口需要以下步骤:1. 确定webservice的地址:通过查看webservice的文档或联系webser...
    99+
    2023-08-22
    webservice
  • python开发接口怎么让第三方调用
    要让第三方调用Python开发的接口,可以采用以下几种方式:1. 使用HTTP协议:Python开发的接口可以通过HTTP协议暴露出...
    99+
    2023-09-28
    python
  • pb怎么调用webservice接口
    调用WebService接口可以通过以下步骤进行: 导入pbwebservice.pbl库文件:在PowerBuilder中,可...
    99+
    2023-10-28
    pb
  • java如何调用webservice接口
    在Java中调用Web服务接口,可以使用Java的SOAP客户端库、REST客户端库或者HTTP客户端库。1. 使用Java的SOA...
    99+
    2023-09-13
    java webservice
  • python开发Webservice服务
    Webservice 记录一下python开发 Web services跨平台,跨语言,跨设备之间的通信。 Web service 三要素 SOAP:SOAP协议 WSDL:Web service描述语言,我的理解是:你服务端的...
    99+
    2023-01-31
    python Webservice
  • 应用Python开发WebService
    Why 得益于Python领域广泛的功能包,使用Python来开发WebService,实现服务端或客户端,是比较快捷的途径。最近项目上恰巧遇到一个问题,内外系统分别作为客户端与服务端,接口功能已基本确定,但目前双方项目进度不统一...
    99+
    2023-01-31
    Python WebService
  • Java webservice接口如何调用
    要调用Java Web服务接口,可以按照以下步骤进行操作: 首先,确保你已经有一个Java Web服务接口的URL地址。 使用Ja...
    99+
    2023-10-23
    Java webservice
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作