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("This is the response.");
			
			// print the response information
			message.writeTo(System.out);
			
			message.writeTo(response.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

web服务端web.xml配置



  
    SOAPResponseServlet
    servlets.SOAPResponseServlet
  

  
    SOAPResponseServlet
    /servlet/SOAPResponseServlet
  

 

 测试类test.Main.java

package test;

import util.SOAPUtil;

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

 

Note:发送的request xml消息如下

This is a request.

 接收的response xml消息如下

This is the response.

 

案例数据运行流程:

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

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

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

 

 

你可能感兴趣的:(SAAJ,SOAP,XML,Web)