In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to understand the soap message with attachments on SAAJ. Many people may not understand it very well. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Soap message api with attachments, which is rich in content, is an API that allows web service to be called with soap messages instead of jax-rpc. It completes the call to web serivce by directly creating XML messages. Soap API simplifies the work of creating XML. The structure diagram of the soap message taken from the j2ee document.
The use of some classes of saaj is not described in detail. Fortunately, they all have good self-explanation.
Package array; import javax.xml.soap.*; import java.net.*; import java.io.*; import java.util.*; import java.text.SimpleDateFormat; public class SaajClient {public SaajClient () {} public static void main (String [] args) throws Exception {SaajClient client = new SaajClient (); User [] user = new User [2] User [0] = new User ("Zhang San", "027-88888888", new Date ()); user [1] = new User ("lisi", null, new Date ()); saajTest (user);} private static void saajTest (User [] user) throws MalformedURLException, IOException, UnsupportedOperationException, SOAPException {MessageFactory factory = MessageFactory.newInstance () / / SAAJ's root factory class SOAPMessage message = factory.createMessage (); / / the SOAPMessage object requires some elements, including the SOAPPart,SOAPEnvelope,SOAPHeader,SOAPBody object / / SAAJ to simplify the operation SOAPFactory s = SOAPFactory.newInstance () by returning a new SOAPMessage object that already includes these elements / / generic factory class, create Name,SOAPElement object Name countUser = s.createName ("countUser", "mh", "http://array"); / / Name object represents a XML qualified name Name arrayOfUser_1 = s.createName (" arrayOfUser_1 "); Name xsi = s.createName (" xmlns:xsi "); Name nullAttribute = s.createName (" xsi:nil ") / / the following code creates the soap object SOAPBody body = message.getSOAPBody (); SOAPBodyElement bodyChildElement = body.addBodyElement (countUser); SOAPElement arrayOfUser = bodyChildElement.addChildElement (arrayOfUser_1); / / arrayOfUser.addAttribute (xsi, "http://www.w3.org/2001/XMLSchema-instance");) ArrayOfUser.addNamespaceDeclaration ("xsi", "http://www.w3.org/2001/XMLSchema-instance"); / / defines the xmlns:xsi attributes of arrayOfUser. This namespace is the XML schema instance namespace, defined by the XML schema specification, which defines / / some features that belong to that namespace that can be used in XML documents. For (int I = 0; I
< user.length; i++) { //需要注意顺序,也就是和复杂类型的sequence元素的顺序对应 Name valueName = s.createName("value"); SOAPElement value = arrayOfUser.addChildElement(valueName); Name birthday = s.createName("birthDay"); SOAPElement birthdayElement = value.addChildElement(birthday); if (user[i].getBirthDay() == null) { birthdayElement.addAttribute(nullAttribute, "1"); } else { //日期类型必须进行格式化 SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); birthdayElement.addTextNode(format.format(user[i].getBirthDay())); } Name name = s.createName("name"); SOAPElement nameElement = value.addChildElement(name); if (user[i].getName() == null) { //传送空值 nameElement.addAttribute(nullAttribute, "1"); } else { nameElement.addTextNode(user[i].getName()); } Name phone = s.createName("phone"); SOAPElement phoneElement = value.addChildElement(phone); if (user[i].getPhone() == null) { phoneElement.addAttribute(nullAttribute, "1"); } else { phoneElement.addTextNode(user[i].getPhone()); } } //发送soap消息 SOAPConnectionFactory f = SOAPConnectionFactory.newInstance(); SOAPConnection conn = f.createConnection(); URL url = new URL("http://localhost:8082/complexType-array/services/CountUser"); SOAPMessage response = conn.call(message, url); SOAPBody soapBody = response.getSOAPBody(); Iterator it = soapBody.getChildElements(); while (it.hasNext()) { SOAPBodyElement bodyElement = (SOAPBodyElement) it.next(); String returnValue = bodyElement.getValue(); System.out.println(bodyElement.getElementName().getLocalName() + " " + returnValue); } response.writeTo(System.out); } } 程序向服务器端传送的数据: POST /complexType-array/services/CountUser HTTP/1.1 SOAPAction: "" Content-Type: text/xml; charset=UTF-8 User-Agent: Java/1.5.0_03 Host: localhost:8082 Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 Connection: keep-alive Content-Length: 448 < env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'> < env:Header/> < env:Body> < mh:countUser xmlns:mh='http://array'> < arrayOfUser_1 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> < value> < birthDay>2006-11-08T22:36:13
< /birthDay> < name>Zhang San
< /name> < phone>027-88888888
< /phone> < /value> < value> < birthDay>2006-11-08T22:36:13
< name>Lisi
< phone xsi:nil='1'/> < /value> < /arrayOfUser_1> < /mh:countUser> < /env:Body>From the point of view of the transmitted data, it is an xml document that conforms to soap specification. Since it is an xml document, that is to say, you can manipulate it with jdom api
As a matter of fact, in J2EE web service development, soap api can be mixed with jdom api.
After reading the above, do you have any further understanding of SAAJ's soap message with attachments? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.