基于Axis 1.4的Web Service入门

news/2024/9/19 13:13:02/文章来源:https://www.cnblogs.com/terry-cc/p/18407384

最近有个客户使用的是Axis 1.4创建的Web Service,很久没用了,所以整理下这块的知识。

基于JDK 1.8和Eclipse Mars开发一个简单的Hello world Web Service

public interface HelloService {

  String hello(String name);
}

public class HelloServiceImpl implements HelloService{

  @Override
  public String hello(String name) {
    return String.format("%s, 您好",name);
  }

}

右键单击HelloService的实现类,选择Web Services -> Create Web Service, 经过下面的配置后,一路Next基本就可以创建客户端项目了。

常规调用方式1:

private HelloServiceImplProxy helloServiceImplProxy = new HelloServiceImplProxy();

String helloTerry = helloServiceImplProxy.hello("程英华的博客");

其他方式:以使用HttpClient为例,这里的重点不是请求方式,而是工具SOAP UI 让我们快速的基于wsdl来创建请求,查看响应。这个工具是本人认为是其他方式的重点。

StringBuilder params = new StringBuilder();
params.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:impl=\"http://impl.soap.hello.penguin.org\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <impl:hello>\n" +
" <impl:name>兰博</impl:name>\n" +
" </impl:hello>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>");

WebServiceClientUtil webServiceClientUtil = new WebServiceClientUtil();
Result<?> result= webServiceClientUtil.createOaSoap(params.toString());


WebServiceClientUtil.java

 1 package org.penguin.hello.util;
 2 
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import org.apache.http.client.config.RequestConfig;
 8 import org.apache.http.client.methods.HttpPost;
 9 import org.apache.http.Header;
10 
11 import org.apache.http.message.BasicHeader;
12 import org.apache.http.impl.client.CloseableHttpClient;
13 import org.apache.http.impl.client.HttpClients;
14 import org.penguin.hello.vo.Result;
15 import org.apache.http.HttpResponse;
16 import org.apache.http.entity.StringEntity;
17 import org.apache.http.util.EntityUtils;
18 import org.dom4j.Document;
19 import org.dom4j.DocumentHelper;
20 import org.dom4j.tree.DefaultElement;
21 
22 import java.nio.charset.StandardCharsets;
23 
24 public class WebServiceClientUtil {
25     /**
26      * Web Service地址
27      */
28     private static final String URL = "http://localhost:8080/helloAxis1/services/HelloServiceImpl";
29 
30     /**
31      * 调用Web Service
32      *
33      * @param params
34      * @return
35      */
36     public static Result<?> createOaSoap(String params) {
37         Result<String> result = Result.ok();
38         // 创建HttpClient实例
39         CloseableHttpClient httpClient = HttpClients.createDefault();
40         // 创建HttpPost实例
41         HttpPost httpPost = new HttpPost(URL);
42         // 连接超时时间,单位:毫秒
43         int connectTimeout = 5000;
44         // 读取超时时间,单位:毫秒
45         int socketTimeout = 5000;
46         RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout)
47                 .setSocketTimeout(socketTimeout).build();
48         httpPost.setConfig(requestConfig);
49         // 设置请求头部
50         List<Header> headers = new ArrayList<>();
51         headers.add(new BasicHeader("SOAPAction", "http://impl.soap.hello.penguin.org/HelloServiceImpl/helloRequest"));
52         httpPost.setHeaders(headers.toArray(new Header[0]));
53         // 设置请求体
54         StringEntity requestEntity = new StringEntity(params, StandardCharsets.UTF_8);
55         requestEntity.setContentType("text/xml");
56         httpPost.setEntity(requestEntity);
57         try {
58             // 执行请求并获得响应
59             HttpResponse response = httpClient.execute(httpPost);
60             // 打印响应状态
61             int statusCode = response.getStatusLine().getStatusCode();
62             // 获取响应内容
63             String responseBody = EntityUtils.toString(response.getEntity());
64             String message = null;
65             String responseString;
66             try {
67                 Document document = DocumentHelper.parseText(responseBody);
68                 responseString = ((DefaultElement) ((DefaultElement) document.content().get(0)).content().get(3))
69                         .element("helloResponse").element("helloReturn").getText();
70                 message = responseString;
71                 result.setResult(message);
72             } catch (Exception e) {
73                 e.printStackTrace();
74                 statusCode = 400;
75                 message = e.getMessage();
76             }
77             result.setCode(statusCode);
78             result.setMessage(message);
79         } catch (Exception e) {
80             result.setCode(400);
81             result.setMessage(e.getMessage());
82             e.printStackTrace();
83         } finally {
84             try {
85                 // 关闭HttpClient连接
86                 httpClient.close();
87             } catch (IOException e) {
88                 e.printStackTrace();
89             }
90         }
91         return result;
92     }
93 }

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/795198.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第四周作业

1、安装burp并实现抓取HTTP站点的数据包(HTTPS站点暂时不要求) 下方练习已完成 2、练习Tomcat PUT方法任意写文件漏洞(CVE-2017-12615),提供蚁剑连接成功截图 # 搜索镜像 docker search cve-2017-12615 # 拉取镜像 docker pull cved/cve-2017-12615 # 查看该镜像的详细信息…

MIT6.824 课程-Raft

Fault Tolerance - Raft 容错模式 我们已经学习了以下几种容错模式(fault-tolerance pattern):计算冗余:MapReduce,但是所有计算由单点 Master 进行调度。 数据冗余:GFS,也是依赖单点 Master 来对多个副本进行选主。 服务冗余:VMware-FT 依赖单个 TestAndSet 操作可以看…

9/10论文学习笔记

1.CPLEX是什么? 2.an apparent-tardiness-cost-with-setup (ATCS)是什么? a basic simulated annealing (SA)基本模拟退火算法 the threshold-accepting (TA) method 阈值接收算法

[Embodied AI Tutorial] The Basic Frameworks and Techniques for Embodied AI (Part2)

目录Embodied AI Tutorial课程内容Modeling and approaches for Embodied AIWorld ModelGet a Good PolicyPlanning And ControlSimulation technology for Embodied AIRigid body simulationCamera simulationAsserts相关链接资料查询 Embodied AI Tutorial 课程主页: slides…

MIT6.824 课程-GFS

GFS原文:https://zhuanlan.zhihu.com/p/113161014 搬运用于参考学习概述 存储(Storage)是一个非常关键的抽象,用途广泛。 GFS 论文还提到了很多关于容错、备份和一致性的问题。 GFS 本身是 Google 内部一个很成功的实用系统,其关键点被很好的组织到一块发表成为了学术论文…

MIT6.824 课程-MapReduce

MapReduce:在大型集群上简化数据处理 概要 MapReduce是一种编程模型,它是一种用于处理和生成大型数据集的实现。用户通过指定一个用来处理键值对(Key/Value)的map函数来生成一个中间键值对集合。然后,再指定一个reduce函数, 它用来合并所有的具有相同中间key的中间value 。…

[Java并发]线程安全的List

线程安全的List 目前比较常用的构建线程安全的List有三种方法:使用Vector容器 使用Collections的静态方法synchronizedList(List< T> list) 采用CopyOnWriteArrayList容器使用Vector容器 Vector类实现了可扩展的对象数组,并且它是线程安全的。它和ArrayList在常用方法的…

章10——面向对象编程(高级部分)——内部类

重点掌握匿名内部类的使用! 1、内部类可以随意访问外部类的成员,包括私有的属性,而外部类不可以。 2、内外部类有重名属性时,内部类的访问采用就近原则,如想访问外部的成员,则用外部类名.this.属性名。内部类分类,四种局部内部类第七条解释:Outer02.this本质是一个外部…

【整理】虚拟地址全解析:操作系统内存管理与进程调度的深度揭秘!

原创 freedom47概述 在现代计算机系统中,虚拟地址是内存管理的关键组成部分。 虚拟地址不仅帮助操作系统高效地管理物理内存,还在进程的内存分配中发挥重要作用。 本文将详细介绍虚拟地址的定义、作用、操作系统的内存管理、进程内存分配、32 位与 64 位架构的内存分配差异,…

2024.9.10 搜索引擎+字体

今天是人工智能的第一节课!我们主要学了引擎的搜索以及字体两部分,干货满满!有一种走了20年弯路的感觉(⊙︿⊙)第一次拥有了博客账号,在我小学的时候我妈妈会用博客记录生活,对于博客有一种熟悉的陌生感hhha 【知识小课堂1】 搜索引擎分为两类: 一、目录式分类搜索引擎,…

The Teachers Day gift a future teacher wants

`#include include void printBanner(); void printHeart(); void printFlower(); int main() { std::cout << "\n"; printBanner(); std::cout << std::endl; printFlower(); std::cout << std::endl; printHeart();return 0;}`点击查看代码 vo…