当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

SAAJ 入门

发表于: 2013-12-01   作者:antlove   来源:转载   浏览次数:
摘要: SAAJ(SOAP with Attachments API for Java) 是在松散耦合软件系统中基于SOAP协议利用XML传递消息的API规.   简单案例 发送SOAP工具类util.SOAPUtil.java package util; import javax.xml.soap.MessageFactory; import javax.xml.soap

SAAJ(SOAP with Attachments API for Java) 是在松散耦合软件系统中基于SOAP协议利用XML传递消息的API规.

 

简单案例

发送SOAP工具类util.SOAPUtil.java

package util;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
public class SOAPUtil {
	private SOAPUtil(){}
	
	public static SOAPUtil newInstance(){
		return new SOAPUtil();
	}
	
	// prepare the request xml 
	private SOAPMessage prepareMessage(String requestXml){
		try {
			MessageFactory messageFactory = MessageFactory.newInstance();
	        SOAPMessage requestMessage = messageFactory.createMessage();
			SOAPPart soapPart = requestMessage.getSOAPPart();
			SOAPEnvelope envelope = soapPart.getEnvelope();
			SOAPBody body = envelope.getBody();
			body.addTextNode(requestXml);
			return requestMessage;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
	
	// get the response xml from response SOAPMessage
	private String detachResponseXml(SOAPMessage responseMessage){
		try {
			return responseMessage.getSOAPPart().getEnvelope().getBody().getTextContent();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}
	
	// connect the remote webservice and return the response xml
	public String fireCall(String requestXml,String url){
		try {
			SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
			SOAPConnection connection = soapConnFactory.createConnection();
			SOAPMessage requestMessage=prepareMessage(requestXml);
			SOAPMessage responseMessage=connection.call(requestMessage, url);
			connection.close();
			return detachResponseXml(responseMessage);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}

}

 

服务类servlets.SOAPResponseServlet.java 

package servlets;

import java.io.BufferedReader;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

public class SOAPResponseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// print the request information
		BufferedReader reader=request.getReader();
		String text=null;
		System.out.println("below is the request information");
		while((text=reader.readLine())!=null){
			System.out.println(text);
		}
		
		// return the response
		try {
			response.setHeader("Content-Type", "text/xml");
			MessageFactory messageFactory = MessageFactory.newInstance();
	        SOAPMessage message = messageFactory.createMessage();
			SOAPPart soapPart = message.getSOAPPart();
			SOAPEnvelope envelope = soapPart.getEnvelope();
			SOAPBody body = envelope.getBody();
			// Add content
			body.addTextNode("<message>This is the response.</message>");
			
			// print the response information
			message.writeTo(System.out);
			
			message.writeTo(response.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

web服务端web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>SOAPResponseServlet</servlet-name>
    <servlet-class>servlets.SOAPResponseServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>SOAPResponseServlet</servlet-name>
    <url-pattern>/servlet/SOAPResponseServlet</url-pattern>
  </servlet-mapping>
</web-app>

 

 测试类test.Main.java

package test;

import util.SOAPUtil;

public class Main {
	public static void main(String[] args) throws Exception {
		String requestXml = "<message>This is a request.</message>";
		String url = "http://localhost:8080/WebServer/servlet/SOAPResponseServlet";
		String responseXml = SOAPUtil.newInstance().fireCall(requestXml, url);
		System.out.println(responseXml);
	}
}

 

Note:发送的request xml消息如下

<SOAP-ENV:Envelope><SOAP-ENV:Header/><SOAP-ENV:Body><message>This is a request.</message></SOAP-ENV:Body></SOAP-ENV:Envelope>

 接收的response xml消息如下

<SOAP-ENV:Envelope><SOAP-ENV:Header/><SOAP-ENV:Body><person><message>This is the response.</message></person></SOAP-ENV:Body></SOAP-ENV:Envelope>

 

案例数据运行流程:

1) 客户端将xml消息装载到http协议中的字节流中,发送到服务器。

2) 服务器解析字节流中信息,并返回数据

3) 客户端解析返回的数据

 

 

SAAJ 入门

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
带附件的soap消息api,其内容很丰富,它是一个允许用soap消息而不是用jax-rpc来调用web service的API
原文: SharePoint 2013 入门教程之入门手册 当我们搭建完环境,创建应用程序和网站集后,就已经正式
深入入门正则表达式(java) - 引言 深入入门正则表达式(java) - 1 - 入门基础 深入入门正则表达
深入入门正则表达式(java) - 引言 深入入门正则表达式(java) - 1 - 入门基础 深入入门正则表达
深入入门正则表达式(java) - 引言 深入入门正则表达式(java) - 1 - 入门基础 深入入门正则表达
WCF快速入门——半自动方式编写WCF入门程序   为什么称为半自动编写方式,主要是因为服务使用系统
Android入门第六篇之ListView (一) 2009-09-11 14:32 75841人阅读 <a style="margin-top: 0px; m
Android入门第六篇之ListView (一) 2009-09-11 14:32 75841人阅读 <a style="margin-top: 0px; m
WCF快速入门——全自动方式编写WCF入门程序 本人认为编写WCF程序有三个方式:   1、全自动方式编
一、概述 Activiti相对Jbpm来说,与Spring整合更加完美,具体可见本文的详细介绍 二、开发环境的准
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号