WSDL远程调用。

迈不过友情╰ 2024-02-24 02:49 118阅读 0赞

上次刚刚提到WSDL的介绍及如何创建WSDL文件及本地文件生成并解析。WSDL详解
接下来,以下内容为远程调用webServic的方法。

发布服务

以下发布服务的代码与上次写的一样,但是TestWsdl类有了一些变化。

  1. package com.test.wsdl;
  2. import javax.xml.ws.Endpoint;
  3. public class Test {
  4. public static void main(String[] args) {
  5. Endpoint.publish("http://localhost:8099/test",new TestWsdl());
  6. }
  7. }

TestWsdl如下,注意要加@WebService,@WebMethod,@WebParam注解

  1. package com.test.wsdl;
  2. import javax.jws.WebMethod;
  3. import javax.jws.WebParam;
  4. import javax.jws.WebService;
  5. @WebService(targetNamespace = "http://wsdl.test.com/")
  6. public class TestWsdl {
  7. @WebMethod(action = "http://wsdl.test.com/testString")
  8. public String testString(@WebParam(name = "str",targetNamespace = "http://wsdl.test.com/") String str){
  9. return "这个是TestString方法的返回内容,这个是你输入的值"+str;
  10. }
  11. }

调用代码

pom依赖

我在处理过程中,出现了依赖冲突,依赖版本与代码不对应的情况,所以直接将全部用到的依赖全部放到下边。

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-test</artifactId>
  9. <scope>test</scope>
  10. </dependency>
  11. <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
  12. <dependency>
  13. <groupId>org.apache.cxf</groupId>
  14. <artifactId>cxf-rt-transports-http</artifactId>
  15. <version>3.3.3</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.cxf</groupId>
  19. <artifactId>cxf-rt-transports-http-jetty</artifactId>
  20. <version>3.2.4</version>
  21. <exclusions>
  22. <exclusion>
  23. <artifactId>cxf-rt-transports-http</artifactId>
  24. <groupId>org.apache.cxf</groupId>
  25. </exclusion>
  26. <exclusion>
  27. <artifactId>cxf-core</artifactId>
  28. <groupId>org.apache.cxf</groupId>
  29. </exclusion>
  30. </exclusions>
  31. </dependency>
  32. <!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
  33. <dependency>
  34. <groupId>org.apache.axis</groupId>
  35. <artifactId>axis</artifactId>
  36. <version>1.4</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.apache.axis</groupId>
  40. <artifactId>axis-jaxrpc</artifactId>
  41. <version>1.4</version>
  42. </dependency>
  43. <!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
  44. <dependency>
  45. <groupId>commons-discovery</groupId>
  46. <artifactId>commons-discovery</artifactId>
  47. <version>0.2</version>
  48. </dependency>
  49. <!-- webservice -->
  50. <dependency>
  51. <groupId>org.apache.cxf</groupId>
  52. <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
  53. <version>3.3.3</version>
  54. <exclusions>
  55. <exclusion>
  56. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  57. <groupId>org.apache.cxf</groupId>
  58. </exclusion>
  59. </exclusions>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.apache.cxf</groupId>
  63. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  64. <version>3.1.12</version>
  65. <exclusions>
  66. <exclusion>
  67. <artifactId>cxf-core</artifactId>
  68. <groupId>org.apache.cxf</groupId>
  69. </exclusion>
  70. </exclusions>
  71. </dependency>
  72. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  73. <dependency>
  74. <groupId>org.projectlombok</groupId>
  75. <artifactId>lombok</artifactId>
  76. <version>1.18.24</version>
  77. <scope>provided</scope>
  78. </dependency>
  79. </dependencies>

调用的时候,也出现引错包导致一直执行不成功的情况,所以将全部的代码贴在下边。

1.首先先定义枚举类,将服务地址,命名空间,方法都可以定义在枚举类中,方便更改名称与统一管理。

  1. package com.test.wsdl;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Getter;
  4. @Getter
  5. @AllArgsConstructor
  6. public enum WSDLEnum {
  7. SERVICE_URL("http://localhost:8099/test?wsdl","服务URL"),
  8. NAMESPACE_URI("http://wsdl.test.com/","命名URI"),
  9. TEST_STRING_METHOD("testString","teseString方法");
  10. private String name;
  11. private String describe;
  12. }

2.客户端

  1. package com.test.wsdl;
  2. import org.apache.axis.Constants;
  3. import org.apache.axis.client.Call;
  4. import org.apache.axis.client.Service;
  5. import javax.xml.namespace.QName;
  6. import javax.xml.rpc.ParameterMode;
  7. public class WebServiceTest {
  8. public static void main(String[] args) {
  9. //服务地址
  10. String url = WSDLEnum.SERVICE_URL.getName();
  11. //命名空间
  12. String namespaceUri = WSDLEnum.NAMESPACE_URI.getName();
  13. //方法名
  14. String method = WSDLEnum.TEST_STRING_METHOD.getName();
  15. try {
  16. Service service = new Service();
  17. Call call = (Call) service.createCall();
  18. call.setTargetEndpointAddress(url);
  19. //设置要调用的方法
  20. call.setOperationName(new QName(namespaceUri, method));
  21. //设置要返回的数据类型
  22. call.setReturnType(new QName(namespaceUri, method), String.class);
  23. call.setUseSOAPAction(true);
  24. call.setSOAPActionURI(namespaceUri + method);
  25. //设置入参
  26. call.addParameter(new QName(namespaceUri, "str"), Constants.XSD_STRING, ParameterMode.IN);
  27. //调用方法并传递参数
  28. String resultStr = (String) call.invoke(new Object[]{
  29. new String("123")});
  30. System.out.println("服务调用结果:" + resultStr);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }

在这里插入图片描述

出现以下展示则调用成功。

发表评论

表情:
评论列表 (有 0 条评论,118人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Feign远程调用

    1. Feign远程调用 > 为啥需要学Feign呢?我们先来回顾之前写的代码 先来看我们以前利用RestTemplate发起远程调用的代码: ![img][] 这

    相关 远程过程调用

    简介 RPC是指远程过程调用,也就是说两台服务器A,B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数/方法,由于不在一个内存空间,不能直接调用,需要通过网络

    相关 远程调用-1

    一、远程调用 考试时不会,求救室友 自己有个方法不会实现,但是别人会实现,让别做的过程,就是远程调用 1.1好处 社会的分工 二、实现调用 Socke