soap的传递和处理

@Test
public void test01(){

String wsdl="http://localhost:8888/ns?wsdl";
String ns="http://service.zttc.org/";
try {
//创建服务
URL url=new URL(wsdl);
QName qname=new QName(ns, "MyserviceImplService");
Service service=Service.create(url, qname);

//创建dispatch
Dispatch<SOAPMessage> dispatch=service.createDispatch(
   new QName(ns,"MyserviceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE);

   //
SOAPMessage soapMessage=MessageFactory.newInstance().createMessage();
SOAPBody soapBody=soapMessage.getSOAPPart().getEnvelope().getBody();
QName bqname=new QName("http://java.zttc.edu.cn/webservice", "add", "ns");
   SOAPBodyElement se=soapBody.addBodyElement(bqname);
   se.addChildElement("a").setValue("20");
   se.addChildElement("b").setValue("20");
   soapMessage.writeTo(System.out);
   
   System.out.println();
   //通过dispatch传递消息
  SOAPMessage soap= dispatch.invoke(soapMessage);
  
 Document doc= soap.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
 String str= doc.getElementsByTagName("addResult").item(0).getTextContent();
  soap.writeTo(System.out);
  System.out.println("----"+str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Test
public void test02(){

String wsdl="http://localhost:8888/ns?wsdl";
String ns="http://service.zttc.org/";

try {
//创建服务
URL url=new URL(wsdl);
QName qname=new QName(ns, "MyserviceImplService");
Service service =Service.create(url, qname);

//创建dispatch分发
QName portName=new QName(ns,"MyserviceImplPort");
Dispatch<Source> dispatch=service.createDispatch(portName, Source.class, Service.Mode.PAYLOAD);

User user=new User(7,"ceshi7","mima7");
JAXBContext jaxbContext=JAXBContext.newInstance(User.class);
Marshaller marshaller=jaxbContext.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
StringWriter sw=new StringWriter();
marshaller.marshal(user, sw);
System.out.println(sw);

String payload="<nn:addUser xmlns:nn=\""+"http://java.zttc.edu.cn/webservice"+"\"/>";
System.out.println(payload);

//创建StreamSource
StreamSource streamSource=new StreamSource(new StringReader(payload));
Source source=dispatch.invoke(streamSource);


Transformer tran=TransformerFactory.newInstance().newTransformer();
DOMResult domResult=new DOMResult();
tran.transform(source, domResult);

XPath xpath=XPathFactory.newInstance().newXPath();
NodeList nl=(NodeList)xpath.evaluate("//user", domResult.getNode(), XPathConstants.NODESET);

User user2=(User) jaxbContext.createUnmarshaller().unmarshal(nl.item(0));
System.out.println(user2.getUsername());
System.out.println(nl.item(0).getNodeName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


@Test
public void test03(){

String wsdl="http://localhost:8888/ns?wsdl";
String ns="http://service.zttc.org/";
try {
//创建服务
URL url=new URL(wsdl);
QName qname=new QName(ns, "MyserviceImplService");
Service service=Service.create(url, qname);

//创建dispatch
Dispatch<SOAPMessage> dispatch=service.createDispatch(
   new QName(ns,"MyserviceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE);

   //
SOAPMessage soapMessage=MessageFactory.newInstance().createMessage();
SOAPBody soapBody=soapMessage.getSOAPPart().getEnvelope().getBody();
QName bqname=new QName("http://java.zttc.edu.cn/webservice", "list", "ns");
   SOAPBodyElement se=soapBody.addBodyElement(bqname);
//    se.addChildElement("a").setValue("20");
//    se.addChildElement("b").setValue("20");
   soapMessage.writeTo(System.out);
   
   System.out.println();
   //通过dispatch传递消息
  SOAPMessage soap= dispatch.invoke(soapMessage);
  
 Document doc= soap.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
//  String str= doc.getElementsByTagName("addResult").item(0).getTextContent();
  soap.writeTo(System.out);
  NodeList nls=doc.getElementsByTagName("user");
  JAXBContext jaxbContext=JAXBContext.newInstance(User.class);
  for (int i = 0; i < nls.getLength(); i++) {
Node n=nls.item(i);
User u=(User) jaxbContext.createUnmarshaller().unmarshal(n);
System.out.println("=========="+u.getUsername());
}
  
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值