Java小白翻身 - webservice教程2

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

来一个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

e5d762e5a4f0523efb6b9f055fc8c4eb.png

步骤 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服务包

8ec7c7d4b204bd117f34fe06c6f7153f.png


步骤 4 登陆接口类

设置一个登陆接口类

f9515b31928ee52ef41dbdfa373e64c6.png


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 登陆接口实现类

94fccfb01ed71c6695df541934bcef97.png


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配置类

785a7a7f428b13c606d6554bb871739f.png


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 异常解决

现在直接启动会报错的。

解决方法如下

096ac2311f3505baaa8fdb793324272b.png


01151c45662d32af0b86edfb52b4c297.png

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

步骤 8 访问webservice

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

22341fba265bd2192eb272b0fec47d3a.png


步骤 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>


相关文章
|
2月前
|
Java 开发者 UED
【实战宝典】Java异常处理大师级教程:throws关键字,让异常声明成为你的专属标签!
【实战宝典】Java异常处理大师级教程:throws关键字,让异常声明成为你的专属标签!
41 3
|
17天前
|
Java API
Java时间戳教程
本文详细介绍Java中时间戳的处理方法,包括获取当前时间戳、使用`java.time`包、时间戳与日期的相互转换及格式化等。示例代码展示了如何利用`System.currentTimeMillis()`和`java.time.Instant`获取时间戳,以及如何通过`Date`和`ZonedDateTime`进行日期转换和时区处理。随着Java 8引入的`java.time`包,日期时间操作变得更加强大和便捷,推荐在新项目中优先采用。
|
2月前
|
前端开发 Java Maven
【前端学java】全网最详细的maven安装与IDEA集成教程!
【8月更文挑战第12天】全网最详细的maven安装与IDEA集成教程!
71 2
【前端学java】全网最详细的maven安装与IDEA集成教程!
|
2月前
|
Java 开发者
Java多线程教程:使用ReentrantLock实现高级锁功能
Java多线程教程:使用ReentrantLock实现高级锁功能
34 1
|
2月前
|
存储 网络协议 Oracle
java教程
java教程【8月更文挑战第11天】
25 5
|
3月前
|
SQL 安全 Java
「滚雪球学Java」教程导航帖(更新2024.07.16)
《滚雪球学Spring Boot》是一个面向初学者的Spring Boot教程,旨在帮助读者快速入门Spring Boot开发。本专通过深入浅出的方式,将Spring Boot开发中的核心概念、基础知识、实战技巧等内容系统地讲解,同时还提供了大量实际的案例,让读者能够快速掌握实用的Spring Boot开发技能。本书的特点在于注重实践,通过实例学习的方式激发读者的学习兴趣和动力,并引导读者逐步掌握Spring Boot开发的实际应用。
74 1
「滚雪球学Java」教程导航帖(更新2024.07.16)
|
2月前
|
Java API
Java与Lua互相调用简单教程
【8月更文挑战第29天】在软件开发中,Java以其强大的稳定性和广泛的生态系统著称,而Lua则因其轻量级、灵活和嵌入式的特点在脚本编写、游戏开发等领域大放异彩。将两者结合使用,可以充分利用Java的底层能力和Lua的快速开发优势。本文将通过一个简单的教程,介绍如何在Java程序中嵌入并执行Lua脚本,以及如何在Lua中调用Java方法。
27 0
WXM
|
3月前
|
Oracle Java 关系型数据库
Java JDK下载安装及环境配置超详细图文教程
Java JDK下载安装及环境配置超详细图文教程
WXM
344 3
|
3月前
|
测试技术 API Android开发
《手把手教你》系列基础篇(九十七)-java+ selenium自动化测试-框架设计篇-Selenium方法的二次封装和页面基类(详解教程)
【7月更文挑战第15天】这是关于自动化测试框架中Selenium API二次封装的教程总结。教程中介绍了如何设计一个支持不同浏览器测试的页面基类(BasePage),该基类包含了对Selenium方法的二次封装,如元素的输入、点击、清除等常用操作,以减少重复代码。此外,页面基类还提供了获取页面标题和URL的方法。
76 2
|
3月前
|
Java 数据安全/隐私保护
Java无模版导出Excel 0基础教程
经常写数据导出到EXCEL,没有模板的情况下使用POI技术。以此作为记录,以后方便使用。 2 工具类 样式工具: 处理工具Java接口 水印工具 导出Excel工具类 3 测试代码 与实际复杂业务不同 在此我们只做模拟 Controller Service 4 导出测试 使用Postman进行接口测试,没接触过Postman的小伙伴可以看我这篇博客Postman导出excel文件保存为文件可以看到导出很成功,包括水印 sheet页名称自适应宽度。还有一些高亮……等功能可以直接搜索使用
Java无模版导出Excel 0基础教程
下一篇
无影云桌面