0


若依分离版——IDEA开发SpringBoot的webservice接口

一.webservice介绍

  1. WebService是帮助多个应用程序与平台和编程语言之间以独立的方式互相通信的标准化技术。它是利用标准 XML messaging(主要 SOAP) 技术,可以访问网络上的其他计算机的记述多种操作的软件接口。此接口主要由 WSDL (WebService Description Language)记述,可以处理多种多样的系统运作。

webservice优点

1)HTTP只能传输字符串类型,需要我们自己解析,而WebService支持复杂的数据类型。

2)传统客户端调用服务器的方法的过程是,发送,接收,解析消息,触发方法,而WebService类似于RPC,不需要我们关心如何发送接收和解析数据,能够像调用本地方法那样调用远程方法。

webservice的缺点

1)不适合相互调用,仅能让客户端调用服务端API,类似于单工通信。

2)相当于HTTP + XML解析,所以会比HTTP还慢,不适合需要高效通信的场景。

3)WebService会降低程序的性能,一台机器或一个局域网里面运行的同构应用程序就不应该用WebService进行通信。

webservice的适用/不适用场景

1)跨防火墙通信

2)应用程序集成:将不同语言写成的在不同平台上运行的各种程序集成起来。

3)软件重用

4)对内的接口不要使用webservice

二. IDEA开发SpringBoot开发webservice接口

本例使用若依通知公告示例

1.引入依赖

在ruoyi-system的pom.xml 引入依赖

  1. <!--webservice start-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web-services</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.cxf</groupId>
  8. <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
  9. <version>3.4.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.apache.cxf</groupId>
  13. <artifactId>cxf-rt-transports-http-jetty</artifactId>
  14. <version>3.4.3</version>
  15. </dependency>
  16. <!--webservice end-->

2. 创建服务端接口

创建服务端接口及接口发布文件

(1)NoticeWebService.java 接口

  1. package com.ruoyi.webservice;
  2. import com.ruoyi.system.domain.SysNotice;
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebService;
  5. import java.util.List;
  6. @WebService(
  7. name = "NoticeWebService", // 暴露服务名称
  8. targetNamespace = "http://service.webservice.com"// 命名空间,一般是接口的包名倒序
  9. )
  10. public interface NoticeWebService {
  11. @WebMethod
  12. List<SysNotice> getList(SysNotice notice);
  13. }

(2)NoticeWebServiceImpl.java 接口

  1. package com.ruoyi.webservice.impl;
  2. import com.ruoyi.system.domain.SysNotice;
  3. import com.ruoyi.system.service.ISysNoticeService;
  4. import com.ruoyi.webservice.NoticeWebService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import javax.jws.WebService;
  8. import java.util.List;
  9. @Service
  10. @WebService(serviceName = "NoticeWebService", // 与接口中指定的name一致, 都可以不写
  11. targetNamespace = "http://service.webservice.com", // 与接口中的命名空间一致,一般是接口的包名倒,都可以不用写
  12. endpointInterface = "com.ruoyi.webservice.NoticeWebService" // 接口类全路径
  13. )
  14. public class NoticeWebServiceImpl implements NoticeWebService {
  15. @Autowired
  16. private ISysNoticeService noticeService;
  17. @Override
  18. public List<SysNotice> getList(SysNotice notice) {
  19. System.out.println("服务被调用 参数:" + notice.getNoticeId());
  20. SysNotice notice1=new SysNotice();
  21. notice1.setNoticeId(Long.valueOf(1));
  22. return noticeService.selectNoticeList(notice1);
  23. }
  24. }

(3)WebServiceConfiguration.java 发布接口

  1. package com.ruoyi.webservice;
  2. import org.apache.cxf.bus.spring.SpringBus;
  3. import org.apache.cxf.jaxws.EndpointImpl;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import javax.xml.ws.Endpoint;
  8. @Configuration
  9. public class WebServiceConfiguration {
  10. @Autowired
  11. private NoticeWebService noticeWebService;
  12. @Autowired
  13. private SpringBus spirngBus;
  14. /**
  15. * 发布服务
  16. * @return
  17. */
  18. @Bean
  19. public Endpoint noticeWebServiceEndpoint(){
  20. System.out.println("服务发布");
  21. //这里指定的端口不能跟应用的端口冲突, 单独指定
  22. String path = "http://127.0.0.1:9090/ws/notice";
  23. EndpointImpl endpoint = new EndpointImpl(spirngBus, noticeWebService);
  24. endpoint.publish(path);
  25. System.out.println("服务成功,path: " + path);
  26. System.out.println(String.format("在线的wsdl:%s?wsdl", path));
  27. return endpoint ;
  28. }
  29. }

(4) 运行后

在浏览器访问接口链接: http://127.0.0.1:9090/ws/notice?wsdl![](https://img-blog.csdnimg.cn/ae5cd276fdfb44cdb3aa84997e9b91a7.png)

如上所示,被调用的接口开发完成。

3. 创建客户端

本文使用的客户端是IDEA2021

(1)在ruoyi-admin下创建webservice目录—>noticeWebService目录,然后打开noticeWebSevice目录,点击菜单栏tools—》WebSevices——》Generate Java Code from Wsdl

输入WSDL地址,输出目录地址,选择Glassfish/jax-ws 2.2RI/Metro 1.x/JWSDP2.2

单击ok后,在目录下自动生成以下文件,主要使用的是NoticeWebService文件和NoticeWebService_Service文件

4. 调用验证

(1)引入单元测试依赖

  1. <!-- 单元测试-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-test</artifactId>
  5. <scope>test</scope>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.junit.jupiter</groupId>
  9. <artifactId>junit-jupiter-engine</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <dependency>
  14. <groupId>junit</groupId>
  15. <artifactId>junit</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-test</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-test</artifactId>
  24. </dependency>
  25. <!-- 单元测试结束-->

(2) 创建MyTest文件

创建MyTest类,给接口传入SysNotice参数,通过NoticeWebService调用接口

  1. package com.ruoyi.web;
  2. import com.ruoyi.web.webservice.noticeWebService.NoticeWebService;
  3. import com.ruoyi.web.webservice.noticeWebService.NoticeWebService_Service;
  4. import com.ruoyi.RuoYiApplication;
  5. import com.ruoyi.web.webservice.noticeWebService.SysNotice;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  9. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  10. import org.springframework.boot.test.context.SpringBootTest;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. import java.util.List;
  13. @RunWith(SpringRunner.class)
  14. @SpringBootTest(classes = RuoYiApplication.class)
  15. @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
  16. public class MyTest {
  17. @Test
  18. public void test()
  19. {
  20. System.out.println("--------调用webservice接口begin-------");
  21. NoticeWebService_Service noticeWebService = new NoticeWebService_Service();
  22. SysNotice notice1=new SysNotice();
  23. notice1.setNoticeId(Long.valueOf(1));
  24. NoticeWebService webService = noticeWebService.getNoticeWebServiceImplPort();
  25. List<SysNotice> list = webService.getList(notice1);
  26. for(int i=0;i<list.size();i++)
  27. System.out.println(list.get(i).getNoticeId()+"===="+list.get(i).getNoticeTitle());
  28. System.out.println("--------调用webservice接口end-------");
  29. }
  30. }

(3)选择MyTest,右键run 运行结果如下,说明调用成功。


本文转载自: https://blog.csdn.net/zhaolulu916/article/details/130726589
版权归原作者 鲸鱼姐 所有, 如有侵权,请联系我们删除。

“若依分离版——IDEA开发SpringBoot的webservice接口”的评论:

还没有评论