最近有个客户使用的是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 }