Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Java小白翻身 - webservice教程2

Java小白翻身 - webservice教程2

作者头像
剽悍一小兔
发布于 2021-07-20 02:21:03
发布于 2021-07-20 02:21:03
80500
代码可运行
举报
运行总次数:0
代码可运行

来一个HelloWorld,SpringBoot发布WebService可简单啦。

步骤 1 搭建项目

请参照这个教程搭建一个SpringBoot项目,注意,项目名字换成webService

image

步骤 2 配置pom.xml

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.6</version>
</dependency>

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.1.6</version>
</dependency>

加上这两个jar包。

步骤 3 建services服务包

image

步骤 4 登陆接口类

设置一个登陆接口类

image

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.webservice.demo.services;
import javax.jws.WebService;
import java.util.Map;

@WebService(name = "LoginService",              // 暴露服务名称
        targetNamespace = "http://java18.cn"    // 命名空间
)
public interface LoginService {

    Map<String,Object> userLogin();

}

步骤 5 登陆接口实现类

image

image

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.webservice.demo.services.impl;

import com.webservice.demo.services.LoginService;

import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;

@WebService(serviceName = "LoginService", // 与接口中指定的name一致
        targetNamespace = "http://java18.cn", // 与接口中的命名空间一致
        endpointInterface = "com.webservice.demo.services.LoginService"// 接口地址
)
public class LoginServiceImpl implements LoginService {

    @Override
    public Map<String, Object> userLogin() {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("errCode",00000);
        resultMap.put("errMsg",null);
        return resultMap;
    }
}

步骤 6 创建CXF配置类

image

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.webservice.demo.config;

import com.webservice.demo.services.LoginService;
import com.webservice.demo.services.impl.LoginServiceImpl;
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;

@Configuration
public class CxfConfig {

    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public LoginService loginService() {
        return new LoginServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), loginService());
        endpoint.publish("/api");
        return endpoint;
    }

}

步骤 7 Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 异常解决

现在直接启动会报错的。

解决方法如下

image

image

这个方法名字换一下就好了。

步骤 8 访问webservice

启动项目,访问http://localhost:8080/webservice/api

image

步骤 9 访问wsdl

http://localhost:8080/webservice/api?wsdl

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://java18.cn" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="LoginService" targetNamespace="http://java18.cn">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://java18.cn" elementFormDefault="unqualified" targetNamespace="http://java18.cn" version="1.0">
<xs:element name="userLogin" type="tns:userLogin"/>
<xs:element name="userLoginResponse" type="tns:userLoginResponse"/>
<xs:complexType name="userLogin">
<xs:sequence/>
</xs:complexType>
<xs:complexType name="userLoginResponse">
<xs:sequence>
<xs:element name="_return">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="entry">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="key" type="xs:string"/>
<xs:element minOccurs="0" name="value" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="userLoginResponse">
<wsdl:part element="tns:userLoginResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="userLogin">
<wsdl:part element="tns:userLogin" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="LoginService">
<wsdl:operation name="userLogin">
<wsdl:input message="tns:userLogin" name="userLogin"> </wsdl:input>
<wsdl:output message="tns:userLoginResponse" name="userLoginResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="LoginServiceSoapBinding" type="tns:LoginService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="userLogin">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="userLogin">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="userLoginResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="LoginService">
<wsdl:port binding="tns:LoginServiceSoapBinding" name="LoginServiceImplPort">
<soap:address location="http://localhost:8080/webservice/api"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
webservice随记
用户1141560
2017/12/26
2.5K0
java调用WebService(一)
因工作需要和一个Sap相关系统以WebService的方式进行接口联调,之前仅听过这种技术,但并没有实操过,所以将本次开发相关的踩坑进行记录
matinal
2023/10/13
8550
WebService入门初使用(二)调用WebService服务
上一节记录完WebService的创建,鉴于篇幅所致,将调用部分单独拿到本节,所调用WebService服务基于上一章节内容。
WindCoder
2018/09/20
1.8K0
WebService入门初使用(二)调用WebService服务
[亲测可用]springBoot调用对方webService接口的几种方法示例
平常我们开发调用接口一般会用到几种数据格式,比如有restful的,这个是目前最流行的,也是最简单开发的,还有一种就是webservice数据格式,这个应该是很久以前的一些项目是用的这种
小小鱼儿小小林
2020/06/29
14.5K0
java接口调用——webservice就是一个RPC而已
  很多新手一听到接口就蒙逼,不知道接口是什么!其实接口就是RPC,通过远程访问别的程序提供的方法,然后获得该方法执行的接口,而不需要在本地执行该方法。就是本地方法调用的升级版而已,我明天会上一篇如何通过socket实现rpc,以及服务的注册和动态上下线。这里先上一篇RPC的实现者一webservice,便于后面理解源码执行过程,框架就是在原理的基础上提供更加便捷的使用而已,协议就是基于TCP或UDP之上,服务者和调用者之间约定消息按照什么样的格式发送以及解析罢了。协议没什么高深莫测的。
intsmaze-刘洋
2018/08/29
2.2K0
java接口调用——webservice就是一个RPC而已
PHP Webservice的发布与调用
发布出来的*.wsdl文件,其实是一个xml格式的文件,生成这个文件可以通过第3方软件,如ZendStudio 就可以生成。
Java架构师必看
2021/03/22
1.4K0
PHP Webservice的发布与调用
Android调用WebService接口详解
我们开发大部分的网络请求都是http来完成的,所以可能有人没有用过WebService,特此写一篇来加深印象并希望可以供大家参考 首先我们需要用到ksoap2的jar包我用的版本是ksoap2-android-assembly-3.5.0-jar-with-dependencies.jar,网上很容易搜到所以我就不贴了,有需要的可以留言。 好了废话不多说了直接上代码
longzeqiu
2019/08/14
1.6K0
WebService: SpringBoot集成WebService实践二
该篇为上一篇的延伸,上一篇知识简单介绍了WebService 的使用,该篇会对代码中模块及功能进行详细介绍,以作对WebService 进一步的理解。
Freedom123
2024/03/29
6050
WebService: SpringBoot集成WebService实践二
WebService客户端调用的5种常见方式
到此一个简单的webservice服务端项目就搭建完成了,这里我在application.properties文件中把端口改成8081:
科技新语
2024/11/21
5580
WebService客户端调用的5种常见方式
记一次在Eclipse中用Axis生成webservice服务端的过程中出现的问题
Unable to find config file. Creating new servlet engine config file: /WEB-INF/server-config.wsdd
JQ实验室
2022/02/09
5890
记一次在Eclipse中用Axis生成webservice服务端的过程中出现的问题
.NET基础拾遗(7)Web Service的开发与应用基础
  Web Service基于SOAP协议,而SOAP本身符合XML语法规范。虽然.NET为Web Service提供了强大的支持,但了解其基本机制对于程序员来说仍然是必需的。
Edison Zhou
2018/08/20
1.7K0
.NET基础拾遗(7)Web Service的开发与应用基础
RPC初探
顺带复习当时使用过的soap协议和wsdl文件,当时使用时还是囫囵吞枣不得其解,今天有幸在周志明老师的书里建立了知识体系,从零散的知识里又拼了一块进去。
燃192
2023/02/28
2.1K0
RPC初探
方法关键字SoapBindingStyle,SoapBodyUse,SoapMessageName,SoapNameSpace
指定此方法用作web方法时使用的绑定样式或SOAP调用机制。仅适用于定义为web服务或web客户端的类。
用户7741497
2022/07/07
5350
利用spring-ws 现实soap webservice服务
背景:有的系统还用soap协议来做webservice.特别要和他们对接的时候。我们需要实现一套。 今天说说,利用spring-ws来(部署,调用)webservcie,能很好的和主流架构(spring-mvc)结合。 参考资料,官方文档https://docs.spring.io/spring-ws/docs/3.0.0.RELEASE/reference/ spring-ws像spring-mvc一样,在集成到web项目时,前端有个servlet分发请求消息的概念。 这个servlet接受soap
技术蓝海
2018/04/26
6K0
利用spring-ws 现实soap webservice服务
常用webservice方法_太极拳初学入门的基本要领
先来考虑一个问题,如果我们要在自己的程序里面展示天气预报,那怎么弄?正确的做法是我们发送一个请求到一个系统,他会给我们返回来天气情况。这个就是一个webservice。天气预报系统就相当于webservice的服务端,我们的系统就相当于客户端。如http://www.webxml.com.cn这个网站上面就列举了多个webservice服务站点
全栈程序员站长
2022/11/08
1.5K0
常用webservice方法_太极拳初学入门的基本要领
SpringBoot WebService服务端&客户端使用教程
知识浅谈
2023/10/19
1.4K0
WCF技术剖析之二十五: 元数据(Metadata)架构体系全景展现[WS标准篇]
元数据实际上是服务终结点的描述,终结点由地址(Address)、绑定(Binding)和契约(Contract)经典的ABC三要素组成。认真阅读过《WCF技术剖析(卷1)》的读者相对会对这三要素的本质有一个深刻的认识:地址决定了服务的位置并实现相应的寻址机制;契约描述了消息交换模式(Message Exchange Pattern: MEP)以及消息的结构(Schema);绑定则通过创建信道栈实现对消息的编码、传输和基于某些特殊的功能(比如实现事务、可靠传输以及基于消息的安全)对消息作出的处理。 服务的消
蒋金楠
2018/01/16
3.1K0
WCF技术剖析之二十五: 元数据(Metadata)架构体系全景展现[WS标准篇]
方法关键字SoapRequestMessage,SoapTypeNameSpace,SqlName,SqlProc
当多个web方法具有相同的SoapAction时使用此方法。 在默认场景中,该关键字指定请求消息的SOAP正文中的顶级元素的名称。 仅适用于定义为web服务或web客户端的类。
用户7741497
2022/07/07
3890
Web-第三十一天 WebService学习【悟空教程】
简单的网络应用使用单一语言写成,它的唯一外部程序就是它所依赖的数据库。大家想想是不是这样呢?
Java帮帮
2018/08/06
2.3K0
Web-第三十一天 WebService学习【悟空教程】
cxf实现webservice_产品框架
WebService是一种跨编程语言和跨操作系统平台的远程调用技术。 跨编程语言和跨操作平台 就是说服务端程序采用java编写,客户端程序则可以采用其他编程语言编写,反之亦然!跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统上运行。 远程调用 就是一台计算机a上的一个程序可以调用到另外一台计算机b上的一个对象的方法,譬如,银联提供给商场的pos刷卡系统,商场的POS机转账调用的转账方法的代码其实是跑在银行服务器上。再比如,amazon,天气预报系统,淘宝网,校内网,百度等把自己的系统服务以WebService服务的形式暴露出来,让第三方网站和程序可以调用这些服务功能,这样扩展了自己系统的市场占有率。 服务端:把公司内部系统的业务方法发布成WebService服务,供远程他人调用 客户端:调用别人发布的WebService服务 常见的远程调动技术: 1) Socket 套接字 TCP/IP UDP 2) WebService 3) http 调用 4) RMI( 远程方法调用 ) Hessian 框架(二进制RPC协议传输数据) WebService 的特点: 1) 跨平台,跨语言 2) W3C(万维网联盟)制定的标准 3) 可以穿透防火墙(因为 soap 协议是基于 HTTP 协议) SOAP 协议(简单对象访问协议Simple Object Access Protocol): WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议 SOAP协议 = HTTP协议 + XML数据格式 WSDL(Web Services Description Language)就是基于XML的语言,用于描述Web Service及其函数、参数和返回值。它是WebService客户端和服务器端都能理解的标准格式。因为是基于XML的,所以WSDL既是机器可阅读的,又是人可阅读的,这将是一个很大的好处。
全栈程序员站长
2022/09/30
1.5K0
cxf实现webservice_产品框架
相关推荐
webservice随记
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验