`

SAAJ 入门

阅读更多

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) 客户端解析返回的数据

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics