日期:2014-05-16  浏览次数:20450 次

SAAJ调用WebService接口--让使用websercice像使用jdbc一样容易
1.让使用websercice像使用jdbc一样容易。

package com.why.client;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeader;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

/**
* SAAJ中使用MTOM方式上传附件时,需设置Content-Type为application/xop+xml; charset=utf-8; type="text/xml",
* 否则服务器端接收不到附件。
*
* @author why
*
*/
public class SoapClient {
public static void main(String[] args) throws Exception{

// printContext();

// selectCustomerByName();

selectMaxAgeCustomer();
}

/**
* 调用一个无参函数
* @throws Exception
*/
public static void printContext() throws Exception{
// 获取SOAP连接工厂
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
// 从SOAP连接工厂创建SOAP连接对象
SOAPConnection connection = factory.createConnection();

// 获取消息工厂
// MessageFactory mFactory = MessageFactory.newInstance();
// 也可用如下方式获取消息工厂
QName serviceName = new QName("http://service.why.com/", "HelloService");
QName portName = new QName("http://service.why.com/", "HelloServicePort");
String endpointAddress = "http://127.0.0.1:8080/helloService";
String operationNameString = "printContext";
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING,endpointAddress);
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class,
Service.Mode.MESSAGE);
BindingProvider bp = (BindingProvider) dispatch;
Map<String, Object> rc = bp.getRequestContext();
rc.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
rc.put(BindingProvider.SOAPACTION_URI_PROPERTY, operationNameString);
SOAPBinding soapBinding = (SOAPBinding)bp.getBinding();
soapBinding.setMTOMEnabled(true);//启用MTOM传输附件方式
MessageFactory mFactory = soapBinding.getMessageFactory();

// 从消息工厂创建SOAP消息对象
SOAPMessage message = mFactory.createMessage();
// 创建SOAPPart对象
SOAPPart part = message.getSOAPPart();
// 创建SOAP信封对象
SOAPEnvelope envelope = part.getEnvelope();
// 创建SOAPHeader对象
SOAPHeader header = message.getSOAPHeader();
// 创建SOAPBody对象
SOAPBody body = envelope.getBody();

// 创建XML的根元素
SOAPBodyElement bodyElementRoot = body.addBodyElement(new QName("http://server.why.com/", "printContext", "ns1"));

// 访问Web服务地址
SOAPMessage reMessage = connection.call(message, new URL("http://127.0.0.1:8080/helloService"));
// 控制台输出返回的SOAP消息
OutputStream os = System.out;
reMessage.writeTo(os);

connection.close();
}

/**
* 调用一个在soap:HEADER中传递参数的函数
* @throws Exception
*/
public s