一.为什么要用soap

  • 原本我们使用web服务都是根据wsdl生成客户端(生成一堆java文件)然后再调用,本章节讲解如何用soap消息来替代这种方式。

二、SOAP消息格式

  • SOAP(简单对象访问协议)是基于XML的消息格式。下面是一个简单的SOAP消息:
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Header>
</soap:Header> <soap:Body> ... message data ... <soap:Fault>
</soap:Fault> </soap:Body> </soap:Envelope>
  • 正如你可以看到一个SOAP消息包括:

    • Envelope
    • Header
    • Body
      • Message Data
      • Fault (optional)

    相同的SOAP消息结构用于客户端和Web Service服务器之间的请求和响应。

    Body内的Fault元素是可选的。只有Web服务中发生内部错误里才返回。否则,返回正常信息数据。

    SOAP不指定一个消息从客户端如何获取到Web Service,但最常见的情况是通过HTTP。

三、案例

  3.1  发布服务

  • 服务接口
package service;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; @WebService
public interface IFirstService { @WebResult(name = "addResult")
public int add(@WebParam(name = "x") int x, @WebParam(name = "y") int y);
}
  • 服务接口实现类
package service;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; @WebService(endpointInterface = "service.IFirstService")
public class IFirstServiceImpl implements IFirstService { @Override
public int add(int x, int y) { return x + y;
} }
  • 发布服务
package publish;

import javax.xml.ws.Endpoint;

import service.IFirstServiceImpl;

public class TestPublish {
public static void main(String[] args) {
Endpoint.publish("http://localhost:3030/first", new IFirstServiceImpl()); System.out.println("发布成功.....");
}
}
  • 查看wsdl文件
<definitions targetNamespace="http://service/" name="IFirstServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://service/" schemaLocation="http://localhost:3030/first?xsd=1" />
</xsd:schema>
</types>
<message name="add">
<part name="parameters" element="tns:add" />
</message>
<message name="addResponse">
<part name="parameters" element="tns:addResponse" />
</message>
<portType name="IFirstService">
<operation name="add">
<input wsam:Action="http://service/IFirstService/addRequest"
message="tns:add" />
<output wsam:Action="http://service/IFirstService/addResponse"
message="tns:addResponse" />
</operation>
</portType>
<binding name="IFirstServiceImplPortBinding" type="tns:IFirstService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document" />
<operation name="add">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="IFirstServiceImplService">
<port name="IFirstServiceImplPort" binding="tns:IFirstServiceImplPortBinding">
<soap:address location="http://localhost:3030/first" />
</port>
</service>
</definitions>
  • http://localhost:3030/first?xsd=1文件
<xs:schema version="1.0" targetNamespace="http://service/">
<xs:element name="add" type="tns:add" />
<xs:element name="addResponse" type="tns:addResponse" />
<xs:complexType name="add">
<xs:sequence>
<xs:element name="x" type="xs:int" />
<xs:element name="y" type="xs:int" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="addResponse">
<xs:sequence>
<xs:element name="addResult" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:schema>

二、客户端发送soap消息、接收soap消息

  • 这里不再用工具生成客户端
package test;

import java.net.URL;
import java.rmi.RemoteException; import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service; import org.w3c.dom.Node; public class TestMain {
public static void main(String[] args) throws RemoteException { /**
* 发送soap消息
*
*/ try {
// 创建一个message工厂
MessageFactory factory = MessageFactory.newInstance();
//获取soapMessage对象
SOAPMessage send_message = factory.createMessage();
SOAPPart soapPart=send_message.getSOAPPart(); /**
* 获取head和body对象
*/
SOAPEnvelope soapEnvelope=soapPart.getEnvelope(); SOAPBody soapBody=soapEnvelope.getBody();
SOAPHeader soapHeader=soapEnvelope.getHeader(); /**
* 把数据封装到body元素里
*/ //添加add标签
QName addName=new QName("http://service/", "add","ns");
SOAPBodyElement soapBodyElement=soapBody.addBodyElement(addName); //添加add标签中的子标签
soapBodyElement.addChildElement(new QName("x")).setValue("1");;
soapBodyElement.addChildElement(new QName("y")).setValue("10");; //打印发送到服务端的soap消息
send_message.writeTo(System.out); /*
* 将消息发送到服务端
*/
URL wsdlDocumentLocation=new URL("http://localhost:3030/first?wsdl");
QName serviceName=new QName("http://service/", "IFirstServiceImplService");
Service service=Service.create(wsdlDocumentLocation, serviceName); /**
* createDispatch方法第一个参数的Qname的构造方法的参数为port标签的name值
* Mode
* Service.Mode.MESSAGE:发送是XML的Doucment对象
* Service.Mode.PAYLOAD:发送的是XML的字符串
*/
QName portName=new QName("http://service/","IFirstServiceImplPort");
Dispatch<SOAPMessage> dispatch=service.createDispatch(portName,SOAPMessage.class,Service.Mode.MESSAGE); //发送soap消息,并接收服务端返回的soap消息
SOAPMessage respon_message=dispatch.invoke(send_message); System.out.println("服务端返回soap消息");
respon_message.writeTo(System.out); /**
* 解析从服务端返回的soap消息
*/
SOAPPart part=respon_message.getSOAPPart();
SOAPEnvelope envelope=part.getEnvelope();
SOAPBody body=envelope.getBody();
Node node=body.getElementsByTagName("addResult").item(0); System.out.println();
System.out.println("result=="+node.getTextContent()); } catch (Exception e) {
e.printStackTrace();
} }
}

结果:

(六)发送、接收SOAP消息(1)的更多相关文章

  1. 2.技巧: 用 JAXM 发送和接收 SOAP 消息—Java API 使许多手工生成和发送消息方面必需的步骤自动化

    转自:https://www.cnblogs.com/chenying99/archive/2013/05/23/3094128.html 技巧: 用 JAXM 发送和接收 SOAP 消息—Java ...

  2. (七)发送、接收SOAP消息(以HttpClient方式)(2)

    一.为什么要用soap 原本我们使用web服务都是根据wsdl生成客户端(生成一堆java文件)然后再调用,本章节讲解如何用soap消息来替代这种方式. 二.SOAP消息格式 SOAP(简单对象访问协 ...

  3. webservice系统学习笔记5-手动构建/发送/解析SOAP消息

    手动拼接SOAP消息调用webservice SOAP消息的组成: 1.创建需要发送的SOAP消息的XML(add方法为例子) /** * 创建访问add方法的SOAP消息的xml */ @Test ...

  4. Spring使用MappingJackson2MessageConverter发送接收ActiveMQ消息

    一.Spring使用JmsTemplate简化对JMS的访问 在JAVA对JMS队列访问中,使用默认的JMS支持将存在大量的检查型异常.通过Spring的支持,可以将所有的JMS的检查型异常转换为运行 ...

  5. Wpf发送接收 win32消息

    #region WPF发送和接收win32消息 public const int WM_GETTEXT = 0x0D; public const int WM_SETTEXT = 0x0C; publ ...

  6. webservice05#soap消息

    1, SOAPMessage结构图 2, SOAP消息的创建 1>前面的一个简单WebService  服务 package com.yangw.soap.service; import jav ...

  7. [3] MQTT,mosquitto,Eclipse Paho---怎样使用 Eclipse Paho MQTT工具来发送订阅MQTT消息?

    在上两节,笔者主要介绍了 MQTT,mosquitto,Eclipse Paho的基本概念已经怎样安装mosquitto. 在这个章节我们就来看看怎样用 Eclipse Paho MQTT工具来发送接 ...

  8. 如何在WCF中用TcpTrace工具查看发送和接收的SOAP消息

    WCF对消息加密(只对消息加密,不考虑Authorize)其实很简单,只要在server和client端的binding加入security mode为Message(还有Transport, Tra ...

  9. 传说中的WCF(4):发送和接收SOAP头

    如果你实在不明白Header是个啥玩意儿,你就想一想你发送电子邮件时,是不是有个叫“附件”的东东?对啊,那么SOAP头是不是可以理解为一种附加信息?就是附加到消息正文的内容. 消息正文又是啥?WCF除 ...

随机推荐

  1. Oracle 11c下载 及连接到OracleDB的简单程序

    Oracle官网总是不太贴心.还是网友贴心. https://pan.baidu.com/s/1ZCFLUi4Ti_WUYOFR3gB2dA 是11g版本下载包,下载下来解压就能用了. 安装完毕后,驱 ...

  2. django 2 ORM操作 ORM进阶 cookie和session 中间件

    ORM操作 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述 ...

  3. 【转】地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02) 的转换算法 C#

    // // Copyright (C) 1000 - 9999 Somebody Anonymous // NO WARRANTY OR GUARANTEE // using System; name ...

  4. sudo内容

    [root@bogon ~]# cat /etc/sudoers## Sudoers allows particular users to run various commands as## the ...

  5. 《Fluid Engine Development》 学习笔记3-光滑粒子流体动力学

    用粒子表示流体最热门的方法就是就是光滑粒子流体动力学(Smoothed Particle Hydrodynamics (SPH).) 这种方法模糊了流体的边界,用有限数量的粒子代表流体,该方法的基本思 ...

  6. Andrew Ng机器学习课程13

    Andrew Ng机器学习课程13 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 引言:主要从一般的角度介绍EM算法及其思想,并推导了EM算法的收敛性.最后 ...

  7. Codis-proxy的配置和启动

    生成配置文件,即将现有的配置文件输出到指定目录位置: ./codis-proxy --default-config | tee conf/proxy.toml 修改配置文件信息: vi conf/pr ...

  8. Java check是否是日期类型

    boolean checkFormate(string parm){ Pattern pattern = Pattern.compile("([0-9]{4})(0[1-9]|1[0-2]) ...

  9. 学习笔记:oracle学习三:SQL语言基础之检索数据:简单查询、筛选查询

    目录 1. 检索数据 1.1 简单查询 1.1.1 检索所有列 1.1.2 检索指定的列 1.1.3 查询日期列 1.1.4 带有表达式的select语句 1.1.5 为列指定别名 1.1.6 显示不 ...

  10. 20175316 盛茂淞 2018-2019-2 《Java程序设计》实验五 《网络安全与编程》 实验报告

    20175316 盛茂淞 2018-2019-2 <Java程序设计>实验五 <网络安全与编程> 实验报告 一.实验报告封面 课程:Java程序设计 班级:1753班 姓名:盛 ...