Java小白翻身 - webservice教程2

article/2025/10/12 20:36:39

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

  • 1、搭建项目
  • 2、配置pom.xml
  • 3、建services服务包
  • 4、登陆接口类
  • 5、登陆接口实现类
  • 6、创建CXF配置类
  • 7、Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 异常解决
  • 8、访问webservice
  • 9、访问wsdl

步骤 1 搭建项目

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

image

步骤 2 配置pom.xml

<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

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

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 {@Overridepublic Map<String, Object> userLogin() {Map<String, Object> resultMap = new HashMap<>();resultMap.put("errCode",00000);resultMap.put("errMsg",null);return resultMap;}
}

步骤 6 创建CXF配置类

image

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 {@Beanpublic ServletRegistrationBean dispatcherServlet() {return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}@Beanpublic LoginService loginService() {return new LoginServiceImpl();}@Beanpublic 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

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>

http://chatgpt.dhexx.cn/article/YhCTkIPx.shtml

相关文章

idea2021创建webservice教程

一、创建服务端 1、创建一个新的java工程 2、工程项目右键添加框架依赖 3、选择webservices 4、点击确定完成创建。创建后项目会添加相关依赖。以及测试服务类。 5、代码介绍 // 添加WebService注解 WebService() public class HelloWorld {// 方法添加WebMethod注解W…

WebService教程

最近在学习WebService&#xff0c;今天尝试用Eclipse的插件生成JAX-WS WebService&#xff0c;结果遇到了不少的问题啊&#xff0c;调试了大半天终于把程序跑通了。现在把步骤和问题记录一下&#xff0c;也为了以后遇到相同的问题时能够及时解决。首先利用Eclipse生成WebServic…

WebService入门教程(服务端发布WebService)

本篇内容过多&#xff0c;时间紧迫的朋友可以通过目录快速筛选自己想要看的内容&#xff0c;本人接触webservice也没多久&#xff0c;也处于学习阶段&#xff0c;如果有错误请指正&#xff0c;如果已经是大神请略过这篇文章&#xff0c;这篇文章不涉及webservice的底层原理&…

WebService最详细教程

WebService相关概念 基础概念 WebService是一种跨编程语言和跨操作系统平台的远程调用技术,就是说服务端程序采用java编写&#xff0c;客户端程序则可以采用其他编程语言编写&#xff0c;反之亦然&#xff01;跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统…

webSerivce简明使用教程

茫茫人海千千万万&#xff0c;感谢这一秒你看到这里。希望我的文章对你的有所帮助&#xff01; 愿你在未来的日子&#xff0c;保持热爱&#xff0c;奔赴山海&#xff01; &#x1f440; 前言 因为前段时间&#xff0c;需要使用到webService来调用公司的其他系统api接口&#x…

WebService详细讲解

1 学习目标2 webservice 基本概念 2.1 什么是web服务2.2 简介2.3 术语 2.3.1 webservice开发规范2.3.2 SOAP 协议2.3.3 wsdl说明书2.3.4 UDDI2.4 应用场景2.5 优缺点 2.5.1 优点&#xff1a;2.5.2 缺点&#xff1a;2.6 面向服务架构SOA3 ApacheCXF 框架介绍 3.1…

11步教你入门webservice

新建立一个javaWeb项目&#xff0c;一个java类&#xff0c;如图&#xff1a; 1.具体很简单 首先要创建一个web工程 2、接下来我们就要将项目中的TestService的这个类生成WebService服务端&#xff0c;选择new Web Service&#xff0c;如图&#xff1a; 3 4.Next,选择jav…

php 实现分页功能(class封装了功能)

前言 分页是一个很常见的功能&#xff0c;我这里提供了分类类(class)&#xff0c;用于前端页面中的四个按钮&#xff1a; 首页下一页上一页尾页 上面的演示非常不直观&#xff0c;但足可以证明这个类可以完成分页功能。 完整的代码 附有非常详细的注释&#xff0c;但需要有…

PHP分页技术详解

直接上代码&#xff0c;注释很详细了。 <?php /** * php分页技术详解 * author jahng */header("Content-type: text/html; charsetUTF-8");echo<link rel"stylesheet" type"text/css" href"css.css" media"all" /&g…

PHP的分页处理

HTML页面&#xff1a; <div class"page"> {$data->render()|raw} </div> 控制器页面&#xff1a; $data ArticleInfo:: where(cate_id, $this->cid)->whereLike(title,%.$get.%)->paginate([ query>[cid>$this->cid], l…

php分页

这是学生信息管理每三条一页> <?phpfunction news($pageNum 1, $pageSize 3){$array array();$mysqlnew mysql();$rs "select * from user limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize;$r $mysql->query($rs);while ($obj…

PHP-分页

1.6 分页 1.6.1 分析 -- 1、获取当前页码的数据 页码 SQL语句 1 select * from products limit 0,10 2 select * from products limit 10,10 3 select * from products limit 20,10 结论&#xff1a; $pageno&#xff1a;页码 $startno:起始位置 $pagesize10:页面大小…

PHP分页查询

1、创建Page.php类&#xff0c;代码如下&#xff1a; <?php /*** 分页模板类*/ class Page {private $cur_page;//当前页private $total;//总条数private $page_size 10;//每页显示的条数private $total_page;//总页数private $first_page;//首页显示名称private $pre_pa…

全功能PHP分页条

网上可以找到的ASP、PHP分页条很多。我也不能免俗&#xff0c;发表一个献献丑。唯一聊以自慰的是这个分页条能生成的显示样式还是很多的&#xff0c;相信能满足大部分人的需要。另一个特点就是使用特别简单&#xff0c;一般传递两个参数即可使用。文档里有使用样例和效果图。发…

php原生分页

自己写一个原生php分页&#xff1a; <?php $linkMySQL_connect(localhost,用户名,密码); mysql_select_db(数据库名称,$link); mysql_query(set names utf8); $resultmysql_query("select * from 表名"); $count mysql_num_rows($result); $Page_size10; …

如何用php实现分页效果

分页效果在网页中是常见的,可是怎样才能实现分页呢,今天做了两种方法来实现一下分页的效果 首先,我们需要准备在数据库里面准备一个表,并且插入数据,这些都是必需的前提工作了,不多说,如图所示(库名为jereh,表名为n_content): 步骤分析: 我们需要分页的话,需要用…

超级好用的PHP分页类

<?phpclass Page {private $total; //总记录private $pagesize; //每页显示多少条private $limit; //limitprivate $page; //当前页码private $pagenum; //总页码private $url; //地址private $bothnum; //两边保持数字分页的量//构造方法初始化pub…

PHP-分页具体实现及代码

数据分页概述 对大量数据进行分页显示是 Web 开发中最常见的情况&#xff0c;但大多刚开始接触 Web 开发的开发人员&#xff0c;对分页技术往往比较迷惘&#xff0c;本节教程以一个分页显示留言板的数据为例就来演示一下 PHP 中基本的数据分页显示原理。 本节教程需要用到的 …

计算机网络原理复习(一)

最近面试某公司的Linux C开发职位&#xff0c;面试的时候面试官提问了一些网络基础的知识&#xff0c;只是惭愧至极&#xff0c;好多知识点已经记忆模糊。周末花时间把网络原理的知识点整理了一下。 计算机网络体系结构&#xff1a; 网络体系结构&#xff1a; 按照我的理解 1…

计算机网络原理练习题及答案

计算机网络原理练习题及答案 读前必看&#xff0c;此篇为计算机网络原理复习对应的练习题。 第一部分传送门&#xff1a;待更新 文章目录 计算机网络原理练习题及答案练习1练习2练习3练习 4练习 5练习 6 练习1 某单位申请到一个B类IP地址&#xff0c;其网络标识&#xff08;N…