SpringBoot——实现WebService接口服务端以及客户端开发

x33g5p2x  于2022-02-22 转载在 Spring  
字(6.7k)|赞(0)|评价(0)|浏览(886)

我们经常需要在两个系统之间进行一些数据的交互,这时候我们就需要开发数据交互接口。

一般来说,遇到比较多的接口有HTTP接口、WebService接口、FTP文件传输。今天我要来学习一下在SpringBoot框架下进行简单的webservice接口的开发。

一、服务端代码开发

创建了两个wbservice接口TestService和CatService。

1、pom依赖

导入相关的依赖包。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web-services</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.apache.cxf</groupId>
  7. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  8. <version>3.1.6</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.apache.cxf</groupId>
  12. <artifactId>cxf-rt-transports-http</artifactId>
  13. <version>3.1.6</version>
  14. </dependency>

2、接口类

  1. import javax.jws.WebMethod;
  2. import javax.jws.WebParam;
  3. import javax.jws.WebResult;
  4. import javax.jws.WebService;
  5. import javax.xml.ws.WebServiceProvider;
  6. @WebService(name = "TestService", // 暴露服务名称
  7. targetNamespace = "http://server.webservice.Bag.admin.com"// 命名空间,一般是接口的包名倒序
  8. )
  9. public interface TestService {
  10. @WebMethod
  11. public String sendMessage(@WebParam(name = "username") String username);
  12. @WebMethod
  13. public boolean getFlag(@WebParam(name = "username") String username);
  14. }
  1. import javax.jws.WebMethod;
  2. import javax.jws.WebParam;
  3. import javax.jws.WebService;
  4. @WebService(name = "CatService", // 暴露服务名称
  5. targetNamespace = "http://server.webservice.Bag.admin.com"// 命名空间,一般是接口的包名倒序
  6. )
  7. public interface CatService {
  8. @WebMethod
  9. public String message(@WebParam(name = "name") String name);
  10. }

3、接口实现类

  1. import com.admin.Bag.webservice.server.TestService;
  2. import org.springframework.stereotype.Component;
  3. import javax.jws.WebService;
  4. @WebService(serviceName = "TestService", // 与接口中指定的name一致
  5. targetNamespace = "http://server.webservice.Bag.admin.com", // 与接口中的命名空间一致,一般是接口的包名倒
  6. endpointInterface = "com.admin.Bag.webservice.server.TestService"// 接口地址
  7. )
  8. @Component
  9. public class TestServiceImpl implements TestService {
  10. @Override
  11. public String sendMessage(String username) {
  12. return "=====Hello! " + username + "=====";
  13. }
  14. @Override
  15. public boolean getFlag(String username) {
  16. //
  17. return true;
  18. }
  19. }
  1. import com.admin.Bag.webservice.server.CatService;
  2. import org.springframework.stereotype.Component;
  3. import javax.jws.WebService;
  4. @WebService(serviceName = "CatService", // 与接口中指定的name一致
  5. targetNamespace = "http://server.webservice.Bag.admin.com", // 与接口中的命名空间一致,一般是接口的包名倒
  6. endpointInterface = "com.admin.Bag.webservice.server.CatService"// 接口地址
  7. )
  8. @Component
  9. public class CatServiceImpl implements CatService {
  10. @Override
  11. public String message(String name) {
  12. //
  13. return "一只小猫猫";
  14. }
  15. }

4、webservice配置文件

  1. import com.admin.Bag.webservice.server.impl.CatServiceImpl;
  2. import com.admin.Bag.webservice.server.impl.TestServiceImpl;
  3. import org.apache.cxf.Bus;
  4. import org.apache.cxf.bus.spring.SpringBus;
  5. import org.apache.cxf.jaxws.EndpointImpl;
  6. import org.apache.cxf.transport.servlet.CXFServlet;
  7. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import javax.xml.ws.Endpoint;
  11. @Configuration
  12. public class cxfConfig {
  13. @Bean
  14. public ServletRegistrationBean disServlet() {
  15. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/webService/*");
  16. return servletRegistrationBean;
  17. }
  18. @Bean(name = Bus.DEFAULT_BUS_ID)
  19. public SpringBus springBus() {
  20. return new SpringBus();
  21. }
  22. @Bean
  23. public Endpoint endpoint() {
  24. EndpointImpl endpoint = new EndpointImpl(springBus(), new TestServiceImpl());
  25. endpoint.publish("/TestService");
  26. return endpoint;
  27. }
  28. @Bean
  29. public Endpoint endpoint2() {
  30. EndpointImpl endpoint = new EndpointImpl(springBus(), new CatServiceImpl());
  31. endpoint.publish("/CatService");
  32. return endpoint;
  33. }
  34. }

启动项目。我的项目端口号是8080。浏览器访问地址:http://localhost:8082/webService
可见接口信息CatService和TestService,点进链接可以看每个接口的wsdl文档。

2、客户端开发

客户端是一个单独的项目。

(1)pom依赖

不同的SpringBoot版本对应的依赖版本也不一样,我也是试了好久终于成了。我的SpringBoot版本号是2.3.0.RELEASE。

  1. <!-- 进行jaxes 服务开发 -->
  2. <dependency>
  3. <groupId>org.apache.cxf</groupId>
  4. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  5. <version>3.0.1</version>
  6. </dependency>
  7. <!-- 内置jetty web服务器 -->
  8. <dependency>
  9. <groupId>org.apache.cxf</groupId>
  10. <artifactId>cxf-rt-transports-http-jetty</artifactId>
  11. <version>3.0.1</version>
  12. </dependency>

(2)封装客户端方法clientUtil

  1. import org.apache.cxf.endpoint.Client;
  2. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
  3. public class clientUtil {
  4. public static String callWebSV(String wsdUrl, String operationName, String... params) throws Exception {
  5. JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
  6. Client client = dcf.createClient(wsdUrl);
  7. //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
  8. Object[] objects;
  9. // invoke("方法名",参数1,参数2,参数3....);
  10. objects = client.invoke(operationName, params);
  11. return objects[0].toString();
  12. }
  13. }

(3)调用接口类

使用定时调用webservice接口。

  1. import com.admin.webAppoint.webservice.client.clientUtil;
  2. import org.springframework.scheduling.annotation.Scheduled;
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. @Component
  7. public class TestController {
  8. //在一个方法中连续调用多次WebService接口,每次调用前需要重置上下文。
  9. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  10. @Scheduled(cron="*/15 * * * * ?")
  11. public String getMessage() {
  12. Thread.currentThread().setContextClassLoader(classLoader);//在获取连接之前 还原上下文
  13. System.out.println("======开始调用webservice接口=====");
  14. String url = "http://localhost:8082/webService/CatService?wsdl";
  15. String methodName = "message";
  16. System.out.println("Calling" + url);
  17. String result="";
  18. try {
  19. result=clientUtil.callWebSV(url, methodName, "name");
  20. } catch (Exception e) {
  21. System.err.println("接口调用失败!!!!");
  22. return "失败";
  23. }
  24. System.out.println("===Finished!===恭喜你啊!!!CatService接口调用成功!!!===获得的数据是:"+result);
  25. return "Finished!";
  26. }
  27. @Scheduled(cron="*/5 * * * * ?")
  28. public String getMessage2() {
  29. Thread.currentThread().setContextClassLoader(classLoader);//在获取连接之前 还原上下文
  30. System.out.println("======开始调用webservice接口=====");
  31. String url = "http://localhost:8082/webService/TestService?wsdl";
  32. String methodName = "sendMessage";
  33. System.out.println("Calling" + url);
  34. String result="";
  35. try {
  36. result=clientUtil.callWebSV(url, methodName, "username");
  37. } catch (Exception e) {
  38. System.err.println("接口调用失败!!!!");
  39. return "失败";
  40. }
  41. System.out.println("===Finished!===恭喜你啊!!!TestService接口调用成功!!!===获得的数据是:"+result);
  42. return "Finished!";
  43. }
  44. }

(4)运行结果

首先启动服务端。启动客户端。

遇到过报错:
报错——使用cxf时报错:org.apache.cxf.interceptor.Fault: Marshalling Error: XXX is not known to this context

最终成功调用服务端的webservice接口:

相关文章