@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();
}
}
soap的传递和处理
最新推荐文章于 2023-04-04 09:45:45 发布