Develop Web service without any framework

Source: Internet
Author: User
Tags soap soapui

I hate the tedious WebService framework configuration.
Especially the Axis series
Rage directly with the servlet to develop
It is also very simple, the key is to get to the request soap and response soap, you can use SOAPUI to generate
The use of SOAPUI here does not introduce

The key to generating the request soap and responding to soap is to parse soap.
Directly using the Java-provided API to parse
Parse Request Soap
The code is as follows:

Java code

  1. Public class Syncnotifyspreqdecoder {

  2. Private Static Logger Logger = Loggerfactory.getlogger (syncnotifyspreqdecoder. Class);

  3. Public Static Syncnotifyspreq decode (InputStream in) {

  4. Documentbuilderfactory domfac=documentbuilderfactory.newinstance ();

  5. String Recordsequenceid = "0";

  6. list<yingzhangreceiptbody> itemList = new arraylist<yingzhangreceiptbody> ();

  7. Try {

  8. Documentbuilder Dombuilder=domfac.newdocumentbuilder ();

  9. Document Doc=dombuilder.parse (in);

  10. Element root=doc.getdocumentelement ();

  11. //Parse Recordsequenceid

  12. NodeList recordsequenceidnodelist = Root.getelementsbytagname ("Recordsequenceid");

  13. if (Recordsequenceidnodelist.getlength () >= 1) {

  14. Recordsequenceid = Recordsequenceidnodelist.item (0). Gettextcontent ();

  15. }

  16. //Parse Item

  17. NodeList itemnodelist = root.getelementsbytagname ("item");

  18. for (int i = 0; i < itemnodelist.getlength (); i++) {

  19. Node item = Itemnodelist.item (i);

  20. String Useridtype = Getnodevalue (item, "Useridtype");

  21. String userId = Getnodevalue (item, "UserId");

  22. String Sp_productid = Getnodevalue (item, "Sp_productid");

  23. String Updatetype = Getnodevalue (item, "Updatetype");

  24. //useridtype fill 1 for mobile phone number; Useridtype 2 for pseudo code

  25. if ( "1". Equals (Useridtype)) {

  26. Yingzhangreceiptbody BODY = new yingzhangreceiptbody (Remove86 (userId), Sp_productid, Escapeservicecode ( Updatetype));

  27. Itemlist.add (body);

  28. }

  29. }

  30. } catch (Exception e) {

  31. Logger.error (E.getmessage (), E);

  32. }

  33. return New Syncnotifyspreq (Recordsequenceid, itemList);

  34. }

  35. //Get node Value

  36. Public Static String Getnodevalue (Node item, string nodeName) {

  37. for (Node n=item.getfirstchild (); n! = null; n=n.getnextsibling ()) {

  38. if (N.getnodetype () = = Node.element_node) {

  39. if (N.getnodename (). Equals (NodeName)) {

  40. return n.gettextcontent ();

  41. }

  42. }

  43. }

  44. return null;

  45. }

  46. }

public class syncnotifyspreqdecoder {private static logger logger =  Loggerfactory.getlogger (Syncnotifyspreqdecoder.class);p Ublic static syncnotifyspreq decode ( Inputstream in) {documentbuilderfactory domfac=documentbuilderfactory.newinstance (); string recordsequenceid =  "0"; List<yingzhangreceiptbody> itemlist = new arraylist<yingzhangreceiptbody> (); Try {documentbuilder dombuilder=domfac.newdocumentbuilder ();D ocument doc=dombuilder.parse (in); Element root=doc.getdocumentelement ();//parsing recordsequenceidnodelist recordsequenceidnodelist =  root.getelementsbytagname ("Recordsequenceid"); if (Recordsequenceidnodelist.getlength ()  >= 1) {recordsequenceid = recordsequenceidnodelist.item (0). Gettextcontent ();} Parse Itemnodelist itemnodelist = root.getelementsbytagname ("item");for  (int i =  0; i <  itemnodelist.getlength ();  i++)  {node item = itemnodelist.item (i); String useridtype = getnodevalue (item,  "Useridtype"); String userid = getnodevalue (item,  "userId"); String sp_productid = getnodevalue (item,  "Sp_productid"); String updatetype = getnodevalue (item,  "Updatetype")//useridtype fill 1  for mobile phone number  ;  useridtype fill 2  as pseudo code if (  "1". Equals (Useridtype)  ) {yingzhangreceiptbody body =  New yingzhangreceiptbody (Remove86 (userId),  sp_productid, escapeservicecode (Updatetype)); Itemlist.add (body);}}}  catch  (exception e)  {logger.error (E.getmessage (),  e);} Return new syncnotifyspreq (recordsequenceid, itemlist);} Get Node valuepublic static string getnodevalue (Node item, string nodename) {for (Node n=item.getfirstchild ();  n != null; n=n.getnextsibling()) {if (N.getnodetype ()  == node.element_node) {if (N.getnodename (). Equals (NodeName)) {return  N.gettextcontent ();}}} Return null;}}


Write Response Soap

Java code

  1. Public class Syncnotifyspresencoder {

  2. /**

  3. * Return SOAP response

  4. * @param recordsequenceid

  5. * @param resultcode 0: success; 1. Failure

  6.  

  7.      */

  8. Public Static String encode (string recordsequenceid, int resultcode) {

  9. StringBuilder ret = new StringBuilder ("<soapenv:envelope xmlns:xsi=\" http://www.w3.org/2001/ Xmlschema-instance\ "xmlns:xsd=\" http://www.w3.org/2001/xmlschema\ "xmlns:soapenv=\" http://schemas.xmlsoap.org/ Soap/envelope/\ "xmlns:soap=\" http://soap.bossagent.vac.unicom.com\ ">");

  10. Ret.append ("<soapenv:Header/>")

  11. . Append ("<soapenv:Body>")

  12. . Append ("<soap:orderrelationupdatenotifyresponse soapenv:encodingstyle=\" Http://schemas.xmlsoap.org/soap /encoding/\ ">")

  13. . Append ("<orderrelationupdatenotifyreturn xsi:type=\" rsp:orderrelationupdatenotifyresponse\ "xmlns:rsp=\" Http://rsp.sync.soap.bossagent.vac.unicom.com\ ">")

  14. . Append ("<recordsequenceid xsi:type=\" soapenc:string\ "xmlns:soapenc=\" http://schemas.xmlsoap.org/soap/ Encoding/\ ">")

  15. . Append (Recordsequenceid)

  16. . Append ("</recordSequenceId>")

  17. . Append ("<resultcode xsi:type=\" xsd:int\ ">")

  18. . Append (ResultCode)

  19. . Append ("</resultCode>")

  20. . Append ("</orderRelationUpdateNotifyReturn>")

  21. . Append ("</soap:orderRelationUpdateNotifyResponse>")

  22. . Append ("</soapenv:Body>")

  23. . Append ("</soapenv:Envelope>");

  24. return ret.tostring ();

  25. }

  26. }

Develop Web service without any framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.