Java--SpringBoot-29-WebService

简介: 在SpringBoot中发布WebService。

在SpringBoot中发布WebService。

什么是WebService?

       webservice就是部署在Web服务器上,向外暴露出能够通过Web进行调用的API服务。可以使用Web(HTTP)方式,接收和响应外部系统的某种请求。

       一个应用程序部署到服务器上,WebService可以对外暴露出一个服务(我有时候叫接口),从而可以让别人来访问这个应用程序。

       网页开发是通过http协议来进行通信,从服务返回的响应统一成一种通用的数据(XML),所以也可以说:WebService=HTTP+XML,WebService 是一种跨编程语言和跨操作系统平台的远程调用技术。

Web services 平台的元素:

SOAP (简易对象访问协议)

UDDI (通用描述、发现及整合)

WSDL (Web services 描述语言)


上代码!

一、添加依赖

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

二、新建一个接口

packagecom.xing.studyboot.webService;
/*** 创建自定义的Service接口* @author xing* @createTime*/publicinterfaceMyService {
Stringhello();
}


实现接口,编写业务

packagecom.xing.studyboot.webService.impl;
importjavax.jws.WebService;
importcom.xing.studyboot.webService.MyService;
@WebServicepublicclassMyServiceImplimplementsMyService{
@OverridepublicStringhello() {
System.out.println("hello,world");
return"ok,hello";
  }
}

三、配置发布服务

packagecom.xing.studyboot.config.webService;
importjavax.xml.ws.Endpoint;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importcom.sun.xml.internal.ws.transport.http.server.EndpointImpl;
importcom.xing.studyboot.webService.MyService;
importcom.xing.studyboot.webService.impl.MyServiceImpl;
@ConfigurationpublicclassWebServiceConfig {
/*** 创建webSocket的 Endpoint* @return*/@BeanpublicEndpointgetEndpoint() {        
MyServiceserviceImpl=newMyServiceImpl();
Endpointpublish=EndpointImpl.publish("http://localhost:8099/hello",serviceImpl);
System.out.println("发布服务成功...");
returnpublish;
  }
}

四、访问发布的服务:

http://localhost:8099/hello?wsdl

image.png

上面的红框框,调用会用到

五、使用PostMan调用:

设置请求头:

Content-Type=text/xml;charset=utf-8

image.png


<?xmlversion='1.0'encoding='utf-8'?><soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:ns1="http://impl.webService.studyboot.xing.com"><!--此处即为命名空间,即targetNamespace--><soapenv:Header/><soapenv:Body><ns1:hello><!--调用方法名称--><HelloName>参数</HelloName><!--调用参数,我们没有设置参数--></ns1:hello></soapenv:Body></soapenv:Envelope>



总结:

       SpringBoot现在都直接在Controller中发布服务,XML也逐渐被JSON替代。

       了解即可。



END

目录
相关文章
|
Java 数据处理
【十二】springboot整合WebService
【十二】springboot整合WebService
1518 0
|
XML Java 数据格式
大多数人忽略了的Spring官方项目,Spring Web Services
大多数人忽略了的Spring官方项目,Spring Web Services
1902 0
|
API UED
如何实现Vue2项目升级Vue3?
如何实现Vue2项目升级Vue3?
1124 157
|
XML Java Maven
WebService客户端调用的5种常见方式
本文介绍了在Java中创建和调用WebService的方法,包括服务端的搭建、配置类的添加以及客户端的多种调用方式(如使用JDK原生代码、wsimport命令、动态调用、代理工厂及HttpClient)。文中详细展示了每种方法的实现步骤和示例代码,强调了服务端与客户端参数实体类字段的兼容性,并推荐使用代理工厂方式进行调用。
4006 0
WebService客户端调用的5种常见方式
|
XML Java 网络架构
使用 Spring Boot 公开 SOAP Web 服务端点:详细指南
使用 Spring Boot 公开 SOAP Web 服务端点:详细指南
2001 0
|
Java API
SpringBoot WebService 及 注意项
SpringBoot WebService 及 注意项
288 0
|
XML 存储 Java
SpringBoot集成WebService
SpringBoot集成WebService
1651 1
|
Java 数据库连接 Apache
SpringBoot整合CXF实现WebService
SpringBoot整合CXF实现WebService
1166 0
|
XML Java 应用服务中间件
WebService - Axis2与Spring整合并发布多个service(同样使用services.xml)
WebService - Axis2与Spring整合并发布多个service(同样使用services.xml)
1313 0