Java learning: Calling Java Web Service

Source: Internet
Author: User

first write a Java class: awbmodel (equivalent to the object to be transmitted in the Web Service)

Package webservicesample; public class awbmodel {private string _ a1 = "_ A1"; Public String geta1 () {return _ A1;} public void seta1 (string value) {This. _ a1 = value;} private string _ a2 = "_ A2"; Public String geta2 () {return _ A2;} public void seta2 (string value) {This. _ a2 = value;} private string a3 = "A3"; Public String geta3 () {return A3;} public void seta3 (string value) {This. a3 = Value ;} Private string A4 = "A4"; Public String geta4 () {return A4;} public void seta4 (string value) {This. a4 = value;} private string A5 = "A5"; Public String geta5 () {return A5;} private string A6 = "A6"; Public void seta6 (string value) {This. a6 = value;} Public String A7 = "A7"; // Private string _ A8 = "A8"; Public String get_a8 () {// return _ A8; return "";} public void set_a8 (string value ){/ /This. _ A8 = value;} Public String sayhelloa () {return "Hello! ";}}

then write an AWB class (equivalent to the Web Service Interface + Implementation)

Package webservicesample; public class AWB {public awbmodel getawbmodelbyno () {awbmodel A = new awbmodel ();. seta1 ("a1_new"); return a;} private string _ b1 = "_ b1"; Public String getb1 () {return _ b1;} public void setb1 (string value) {This. _ b1 = value;} private string _ b2 = "_ b2"; Public String getb2 () {return _ b2;} public void setb2 (string value) {This. _ b2 = value;} private string B3 =" B3 "; Public String getb3 () {return B3;} public void setb3 (string value) {This. b3 = value;} private string B4 = "B4"; Public String getb4 () {return B4;} public void setb4 (string value) {This. b4 = value;} private string B5 = "B5"; Public String getb5 () {return B5;} private string B6 = "B6"; Public void setb6 (string value) {This. b6 = value;} Public String B7 = "B7"; Public String sayhello B () {return "Hello B! ";}}

After the Web Service is encapsulated in the jbuilder2006 environment and published to WebLogic, the content of the WSDL can be found in:

Note:BecauseA5 only has the get Method,A6 only has the Set MethodThe resulting WSDL automatically removes these two "attributes" (this will be generated in the true sense when C # is used to call the Java Web service.. Net Property), other custom methods (such as sayhelloa) are discarded (that is, these methods are not automatically serialized), and it is interesting that"_ A8", Awbmodel. java only defines the set_a8 (string value) and get_a8 () methods, and does not declare the private variable of _ A8 at all. However, Java is automatically considered as a private member with "_ A8" already exists; in addition, the public variables declared directly do not change the case sensitivity in WSDL, and they are stillA7The other setxxx and getxxx methods are all removed from set/get, and the first letter is lowercase, that isXXX.

After adding a service reference in vs.net, client calls such as xxxclient, xxxchannel, and proxy class are automatically generated.CodeSimilarly, after make, JBuilder will also generate the client code sample package awb_client.jar. decompress the package and check the content of awbmodel. Java:

/*** This code was automatically generated at 5:48:57 on 2012-12-4 * by weblogic. XML. schema. binding. internal. codegen. beanimplgenerator -- Do not edit. ** @ version WebLogic Server 9.2 sp0 Mon Jul 31 05:48:10 PDT 2006 796124 * @ author copyright (c) 2012 by BEA Systems, Inc. all rights reserved. */package webservicesample. generated; // Original Type: ['java: webservicesample ']: awbmodelpublic class AWB Model implements Java. io. serializable {public awbmodel () {} public awbmodel (Java. lang. string P _ A8, Java. lang. string p_a1, Java. lang. string p_a2, Java. lang. string p_a3, Java. lang. string p_a4, Java. lang. string p_a7) {This. _ A8 = P _ A8; this. a1 = p_a1; this. a2 = p_a2; this. a3 = p_a3; this. a4 = p_a4; this. a7 = p_a7;} private Java. lang. string _ A8;/** <br> derived from _ A8. <br> schema name = ['java: Webservicesample ']: _ A8 <br> schema type = ['HTTP: // www.w3.org/2001/xmlschema'{:string <br> schema formqualified = true */Public Java. lang. string get_a8 () {return _ A8;} public void set_a8 (Java. lang. string v) {This. _ A8 = V;} private Java. lang. string A1;/** <br> derived from A1. <br> schema name = ['java: webservicesample ']: A1 <br> schema type = ['HTTP: // www.w3.org/2001/xmlschema'{:string <br> Schema formqualified = true */Public Java. lang. string geta1 () {return A1;} public void seta1 (Java. lang. string v) {This. a1 = V;} private Java. lang. string A2;/** <br> derived from A2. <br> schema name = ['java: webservicesample ']: A2 <br> schema type = ['HTTP: // www.w3.org/2001/xmlschema'{:string <br> schema formqualified = true */Public Java. lang. string geta2 () {return A2;} public void Seta 2 (Java. lang. string v) {This. a2 = V;} private Java. lang. string A3;/** <br> derived from A3. <br> schema name = ['java: webservicesample ']: A3 <br> schema type = ['HTTP: // www.w3.org/2001/xmlschema'{:string <br> schema formqualified = true */Public Java. lang. string geta3 () {return A3;} public void seta3 (Java. lang. string v) {This. a3 = V;} private Java. lang. string A4;/** <br> derived from A4. <Br> schema name = ['java: webservicesample ']: A4 <br> schema type = ['HTTP: // www.w3.org/2001/xmlschema':string <br> schema formqualified = true */Public Java. lang. string geta4 () {return A4;} public void seta4 (Java. lang. string v) {This. a4 = V;} private Java. lang. string A7;/** <br> derived from A7. <br> schema name = ['java: webservicesample ']: A7 <br> schema type = ['HTTP: // www.w3.org/2001/xml Scheme']: String <br> schema formqualified = true */Public Java. lang. string geta7 () {return A7;} public void seta7 (Java. lang. string v) {This. a7 = V;} public Java. lang. string tostring () {return "awbmodel" + "{" + "_ A8 = <" + get_a8 () + ">" + "a1 = <" + geta1 () + ">" + "A2 = <" + geta2 () + ">" + "A3 = <" + geta3 () + ">" + "A4 = <" + geta4 () + ">" + "A7 = <" + geta7 () + ">" + "}";} public Boolean EQ Uals (Java. lang. object _ other _) {If (_ other _ = This) return true; If (_ other _ instanceof awbmodel) {awbmodel _ OBJ _ = (awbmodel) _ other __; return true & (_ A8 = NULL? _ OBJ _. get_a8 () = NULL: _ a8.equals (_ OBJ _. get_a8 () & (a1 = NULL? _ OBJ _. geta1 () = NULL: a1.equals (_ OBJ _. geta1 () & (A2 = NULL? _ OBJ _. geta2 () = NULL: a2.equals (_ OBJ _. geta2 () & (A3 = NULL? _ OBJ _. geta3 () = NULL: a3.equals (_ OBJ _. geta3 () & (A4 = NULL? _ OBJ _. geta4 () = NULL: a4.equals (_ OBJ _. geta4 () & (A7 = NULL? _ OBJ __. geta7 () = NULL: a7.equals (_ OBJ __. geta7 ();} return false;} public int hashcode () {int _ Hash _ result _ = 17; _ Hash _ result _ = 37 * _ Hash _ result _ + (_ A8 = NULL? 0: _ a8.hashcode (); _ Hash _ result _ = 37 * _ Hash _ result _ + (a1 = NULL? 0: a1.hashcode (); _ Hash _ result _ = 37 * _ Hash _ result _ + (A2 = NULL? 0: a2.hashcode (); _ Hash _ result _ = 37 * _ Hash _ result _ + (A3 = NULL? 0: a3.hashcode (); _ Hash _ result _ = 37 * _ Hash _ result _ + (A4 = NULL? 0: a4.hashcode (); _ Hash _ result _ = 37 * _ Hash _ result _ + (A7 = NULL? 0: a7.hashcode (); Return _ Hash _ result __;}}

It is basically the same as the one seen in the WSDL, but there are some interesting small changes compared with the original awbmodel. Java: defined in the original awbmodel. JavaPrivate member, Always changedLowercaseAnd the automatically added"_ A8"But againUppercase.

The following describes how to call Web Service in Java:

Create a Java project and reference the awb_client.jar package automatically generated by JBuilder.

Package jmyang. web_service_call; import webservicesample. generated. *; import Java. io. ioexception; import javax. XML. RPC. serviceexception; public class wscalldemo {public void callservice () {string wsdlurl = "http: // 127.0.0.1: 7001/mywebservice/AWB? WSDL "; try {AWB service = new awb_impl (wsdlurl); awbport Port = service. getawbport (); awbmodel result = port. getawbmodelbyno (); system. out. println ("awbmodel. a1 = "+ result. geta1 ();} catch (ioexception e) {e. printstacktrace ();} catch (serviceexception E2) {e2.printstacktrace ();}}}

In this way, you can call it.

Of course,. Net can also call the Web Service written in Java. Add service reference in vs.net. Enter the service address http: // 127.0.0.1: 7001/mywebservice/AWB? WSDL vs.net will automatically generate other code, such as the proxy class, like adding a reference to the WCF/asmx service. Let's look at the automatically generated awbmodel class.

/// <Remarks/> [system. codedom. compiler. generatedcodeattribute ("system. XML "," 4.0.30319.225 ")] [system. serializableattribute ()] [system. diagnostics. debuggerstepthroughattribute ()] [system. componentmodel. designercategoryattribute ("Code")] [system. XML. serialization. soaptypeattribute (namespace = "Java: webservicesample")] public partial class awbmodel: object, system. componentmodel. inotifypropertychange D {private string _ a8field; private string a1field; private string a2field; private string a3field; private string a4field; private string a7field; // <remarks/> [system. XML. serialization. soapelementattribute (isnullable = true)] Public String _ A8 {get {return this. _ a8field;} set {This. _ a8field = value; this. raisepropertychanged ("_ A8") ;}/// <remarks/> [system. XML. serialization. soapelementat Trigger (isnullable = true)] Public String A1 {get {return this. a1field;} set {This. a1field = value; this. raisepropertychanged ("A1") ;}/// <remarks/> [system. XML. serialization. soapelementattribute (isnullable = true)] Public String A2 {get {return this. a2field;} set {This. a2field = value; this. raisepropertychanged ("A2") ;}/// <remarks/> [system. XML. serialization. soapelementattribute (isnull Able = true)] Public String A3 {get {return this. a3field;} set {This. a3field = value; this. raisepropertychanged ("A3") ;}/// <remarks/> [system. XML. serialization. soapelementattribute (isnullable = true)] Public String A4 {get {return this. a4field;} set {This. a4field = value; this. raisepropertychanged ("A4") ;}/// <remarks/> [system. XML. serialization. soapelementattribute (isnullable = true)] Pu BLIC string A7 {get {return this. a7field;} set {This. a7field = value; this. raisepropertychanged ("A7") ;}} public event system. componentmodel. propertychangedeventhandler propertychanged; protected void raisepropertychanged (string propertyname) {system. componentmodel. propertychangedeventhandler propertychanged = This. propertychanged; If (propertychanged! = NULL) {propertychanged (this, new system. componentmodel. propertychangedeventargs (propertyname ));}}}

We can see that A1, A2, A3, A4, A7, and _ A8 all appear in the property mode.

The calling code of C # is simple as follows:

 
Using (yjm. awbportclient client = new awbportclient () {VAR model = client. getawbmodelbyno (); console. writeline ("a1 =" + model. a1); console. writeline ("A2 =" + model. a2); console. writeline ("A3 =" + model. a3); console. writeline ("A4 =" + model. a4); console. writeline ("A7 =" + model. a7); console. writeline ("_ A8 =" + model. _ A8); console. read ();}

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.