首页 技术 正文
技术 2022年11月15日
0 收藏 304 点赞 4,947 浏览 5734 个字

一、创建并发布一个简单的webservice应用

  1、webservice 代码:

  

 package com.ls.demo; import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint; @WebService
public class HelloWorld {
@WebMethod
public String sayHello(String str){
System.out.println("get Message...");
String result = "Hello World, "+str;
return result;
}
public static void main(String[] args) {
System.out.println("server is running");
String address="http://localhost:9000/HelloWorld";
Object implementor =new HelloWorld();
Endpoint.publish(address, implementor);
} }

  2、运行项目,并访问 “http://localhost:9000/HelloWorld?wsdl”,得到如下wsdl文件,说明webservice发布成功:

  

 <?xml version="1.0" encoding="UTF-8"?>
<!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
<!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://demo.ls.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://demo.ls.com/" name="HelloWorldService">
<types>
<xsd:schema>
<xsd:import namespace="http://demo.ls.com/" schemaLocation="http://localhost:9000/HelloWorld?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"></part>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"></part>
</message>
<portType name="HelloWorld">
<operation name="sayHello">
<input wsam:Action="http://demo.ls.com/HelloWorld/sayHelloRequest" message="tns:sayHello"></input>
<output wsam:Action="http://demo.ls.com/HelloWorld/sayHelloResponse" message="tns:sayHelloResponse"></output>
</operation>
</portType>
<binding name="HelloWorldPortBinding" type="tns:HelloWorld">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="sayHello">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
<service name="HelloWorldService">
<port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">
<soap:address location="http://localhost:9000/HelloWorld"></soap:address>
</port>
</service>
</definitions>

二、客户端访问webservice

  1、通过 HttpClient 及  HttpURLConnection 发送SOAP请求,代码如下:

  

 import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils; public class TestHelloWrold {
public static void main(String[] args) throws Exception {
String wsdl = "http://localhost:9000/HelloWorld?wsdl";
int timeout = 10000;
StringBuffer sb = new StringBuffer("");
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<soap:Envelope "
+ "xmlns:api='http://demo.ls.com/' "
+ "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
+ "xmlns:xsd='http://www.w3.org/2001/XMLSchema' "
+ "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>");
sb.append("<soap:Body>");
sb.append("<api:sayHello>");
sb.append("<arg0>ls</arg0>");
sb.append("</api:sayHello>");
sb.append("</soap:Body>");
sb.append("</soap:Envelope>"); // HttpClient发送SOAP请求
System.out.println("HttpClient 发送SOAP请求");
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(wsdl);
// 设置连接超时
client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
// 设置读取时间超时
client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
// 然后把Soap请求数据添加到PostMethod中
RequestEntity requestEntity = new StringRequestEntity(sb.toString(), "text/xml", "UTF-8");
//设置请求头部,否则可能会报 “no SOAPAction header” 的错误
postMethod.setRequestHeader("SOAPAction","");
// 设置请求体
postMethod.setRequestEntity(requestEntity);
int status = client.executeMethod(postMethod);
// 打印请求状态码
System.out.println("status:" + status);
// 获取响应体输入流
InputStream is = postMethod.getResponseBodyAsStream();
// 获取请求结果字符串
String result = IOUtils.toString(is);
System.out.println("result: " + result); // HttpURLConnection 发送SOAP请求
System.out.println("HttpURLConnection 发送SOAP请求");
URL url = new URL(wsdl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(timeout);
conn.setReadTimeout(timeout); DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.write(sb.toString().getBytes("utf-8"));
dos.flush(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line = null;
StringBuffer strBuf = new StringBuffer();
while ((line = reader.readLine()) != null) {
strBuf.append(line);
}
dos.close();
reader.close(); System.out.println(strBuf.toString());
} }

  响应报文如下:

  

<?xml version="1.0" ?>
  <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
      <ns2:sayHelloResponse xmlns:ns2="http://demo.ls.com/">
        <return>Hello World, ls</return>
      </ns2:sayHelloResponse>
    </S:Body>
  </S:Envelope>

  问:SOAP的请求报文的格式是怎么来的呢?

  答:可用Eclipse测试WSDL文件,则可得到想要的SOAP请求及响应报文,具体步骤如下图:

第一步:

Java发布一个简单 webservice应用   并发送SOAP请求

第二步:

通过第一步,会在浏览器打开如下的页面

Java发布一个简单 webservice应用   并发送SOAP请求

  

  2、生成客户端代码访问

   a、通过 “wsimport”(JDK自带)命令生成客户端代码。进入命令行模式,执行 wsimport -s . http://localhost:9000/HelloWorld?wsdl,就会在当前目录下生成客户端代码。附图:

Java发布一个简单 webservice应用   并发送SOAP请求

  b、通过Eclipse生成客户端代码

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298