Java WebService call Methods

Source: Internet
Author: User
Tags wsdl
I. Use JDK Web Service APIs. Here we use a web service based on SOAP message.
1. Create a web services endpoint: Package hello;
Import javax. JWS. WebService;
Import javax. JWS. webmethod;
Import javax. xml. ws. endpoint;

@ WebService
Public class Hello {

@ Webmethod
Public String Hello (string name ){
Return "hello," + name + "\ n ";
}

Public static void main (string [] ARGs ){
// Create and publish an endpoint
Hello = new Hello ();
Endpoint endpoint = endpoint. Publish ("http: // localhost: 8080/Hello", hello );
}
}
2. Use apt to compile hello. Java (for example, Apt-d [to store the compiled file directory] Hello. Java). The Jaws directory is generated.
3. Run the program using Java hello. Hello, And then direct the browser to http: // localhost: 8080/Hello? The following displays the WSDL

4. Use wsimport to generate the client

Use the following command: wsimport-P.-Keep http: // localhost: 8080/Hello? WSDL

The following file is generated in the current directory:

5. Client Program:
1 class helloclient {
2 public static void main (string ARGs []) {
3 helloservice service = new helloservice ();
4 Hello helloproxy = service. gethelloport ();
5 string Hello = helloproxy. Hello ("hello ");
6 system. Out. println (Hello );
7}
8}
9
The above methods are a little complicated, and there are simpler methods

2. Use xfire. Here I use xfire integrated with myeclipse for testing.
You can develop WebService using xfire in three ways:
One is generated from the JavaBean;
Two are generated from the WSDL file;
3. Create a WebService by yourself.
The procedure is as follows:
Use myeclipse to create a WebService project. The directory structure is as follows:

First, create a WebService interface,
The Code is as follows:

1 package com. myeclipse. wsexample;
2 // generated by myeclipse
3
4 public interface ihelloworldservice {
5
6 Public String example (string message );
7
8} Then I implemented this excuse: 1 package com. myeclipse. wsexample;
2 // generated by myeclipse
3
4 public class helloworldserviceimpl implements ihelloworldservice {
5
6 Public String example (string message ){
7 return message;
8}
9
10} modify the service. xml file and add the following code:
1 <service>
2 <Name> helloworldservice </Name>
3 <serviceclass>
4 com. myeclipse. wsexample. ihelloworldservice
5 </serviceclass>
6 <implementationclass>
7 COM. myeclipse. wsexample. helloworldserviceimpl
8 </implementationclass>
9 <style> wrapped </style>
10 <use> literal </use>
11 <scope> application </scope>
12 </service> deploy the entire project to the Tomcat server. Open the browser and enter http: // localhost: 8989/helloworld/services/helloworldservice? WSDL, you can see the following:

Expand the WSDL following helloworldservice and you will see:

The client implementation is as follows: 1 package com. myeclipse. wsexample. client;
2
3 Import java.net. malformedurlexception;
4 Import java.net. url;
5
6 Import org. codehaus. xfire. xfirefactory;
7 Import org. codehaus. xfire. Client. client;
8 Import org. codehaus. xfire. Client. xfireproxyfactory;
9 Import org. codehaus. xfire. Service. Service;
10 Import org. codehaus. xfire. Service. Binding. objectservicefactory;
11
12 Import com. myeclipse. wsexample. ihelloworldservice;
13
14 public class helloworldclient {
15 public static void main (string [] ARGs) throws malformedurlexception, exception {
16 // todo auto-generated method stub
17 service S = new objectservicefactory (). Create (ihelloworldservice. Class );
18 xfireproxyfactory XF = new xfireproxyfactory (xfirefactory. newinstance (). getxfire ());
19 string url = "http: // localhost: 8989/helloworld/services/helloworldservice ";
20
21 try
22 {
23
24 ihelloworldservice HS = (ihelloworldservice) XF. Create (S, URL );
25 string ST = HS. Example ("zhangjin ");
26 system. Out. Print (ST );
27}
28 catch (exception E)
29 {
30 E. printstacktrace ();
31}
32}
33
34}
35. Let's talk about the problem here. Sometimes we know a wsdl address, for example, we want to use a Java client to reference it. net WebService, which is referenced by myeclipse, but fails to pass the verification. In this case, we can directly reference it in the class. The steps are as follows:
1 public static void main (string [] ARGs) throws malformedurlexception, exception {
2 // todo auto-generated method stub
3 service S = new objectservicefactory (). Create (ihelloworldservice. Class );
4 xfireproxyfactory XF = new xfireproxyfactory (xfirefactory. newinstance (). getxfire ());
5
6
7 // remotely call the WebService developed by. net
8 client c = new client (new URL ("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx? WSDL "));
9 object [] O = C. Invoke ("qqcheckonline", new string [] {"531086641", "591284436 "});
10
11 // call the WebService developed by the. NET Local Machine
12 client C1 = new client (new URL ("http: // localhost/ZJ/service. asmx? WSDL "));
13 object [] O1 = c1.invoke ("helloworld", new string [] {});
14
15}
3. Use axis1.4 to call the WebService Method
Prerequisites: Download The axis1.4 package and the Tomcat server, and copy the axis folder to the webapp folder of the Tomcat server.
Here I will talk about the simplest method:
First, create an arbitrary Java class (for example, helloworld. java), copy to the axis folder, change its extension to JWS, restart tomcat, and enter http: // localhost: 8989/axis/helloworld in the browser. JWS? To Get a wsdl file. The client call method is as follows:
1 import javax. xml. rpc. Service;
2 Import javax. xml. rpc. serviceexception;
3 Import javax. xml. rpc. servicefactory;
4
5 import java.net. malformedurlexception;
6 Import java.net. url;
7 Import java. RMI. RemoteException;
8
9 Import javax. xml. namespace. QNAME;
10
11 public class testhelloworld {
12
13
14 public static void main (string [] ARGs) throws malformedurlexception, serviceexception, RemoteException {
15 // todo auto-generated method stub
16
17 string wsdlurl = "http: /localhost: 8989/axis/helloworld. JWS? WSDL ";
18 string namespaceuri = "http: // localhost: 8989/axis/helloworld. JWS ";
19 string servicename = "helloworldservice ";
20 string portname = "helloworld ";
21
22 servicefactory = servicefactory. newinstance ();
23 service afservice = servicefactory. createservice (new URL (wsdlurl), new QNAME (namespaceuri, servicename ));
24 helloworldinterface proxy = (helloworldinterface) afservice. getport (New QNAME (namespaceuri, portname), helloworldinterface. Class );
25 system. Out. println ("return value is" + proxy. getname ("John "));
26
27}
28
29}
30. Use axis2 to develop WebService (Thank you, Mr. Li Ning)
To use axis2, you must first download
Axis2-1.4.1-bin.zip

Axis2-1.4.1-war.zip

Http://ws.apache.org/axis2/
Similarly, you also need to copy axis2 to the webapp directory.
There are two ways to deploy WebService in axis2,
The first method is the pojo method. This method is relatively simple, but there are some restrictions. For example, the deployed class cannot contain the package name.
The second method is to use XML to publish WebService. This method is flexible and does not require a restriction class declaration.
The following describes the usage:
1. pojo mode: In axis2, you can directly publish a simple pojo as a WebService without any configuration. All the public methods in pojo will be published as WebService methods. First implement a pojo class:
1 public class helloworld {
2 Public String getname (string name)
3 {
4 return "hello" + name;
5}
6 Public int add (int A, int B)
7 {
8 return A + B;
9}
10}
11 since both methods are public, they are all published as WebService. After the helloworld class is compiled, set helloworld. put the class file in the % Tomcat % \ webapps \ axis2 \ WEB-INF \ pojo directory (if there is no pojo directory, create the Directory) and open the browser to test:
Enter the URL:
HTTP: /localhost: 8080/axis2/services/listservices

All WebServices are listed.

Here is the list of two WebServices, and then test on the client:
First, you can write an encapsulation class to reduce the encoding. The Code is as follows:

1 package MZ. getwebservice;
2 Import javax. xml. namespace. QNAME;
3
4 Import org. Apache. axis2.axisfault;
5 import org. Apache. axis2.addressing. endpointreference;
6 Import org. Apache. axis2.client. options;
7 Import org. Apache. axis2.rpc. Client. rpcserviceclient;
8
9
10 public class getwsbyaxis2 {
11 Private Static string endpointurl;
12 Private Static string qurl = "http://ws.apache.org/axis2 ";
13 private QNAME opaddentry;
14 Public String wsurl;
15 public rpcserviceclient setoption () throws axisfault
16 {
17 rpcserviceclient serviceclient = new rpcserviceclient ();
18 options Options = serviceclient. getoptions ();
19 endpointreference targetepr = new endpointreference (wsurl );
20 options. setto (targetepr );
21 return serviceclient;
22}
23
24 public QNAME getqname (string option ){
25
26 return New QNAME (qurl, option );
27}
28 // return string
29 public string getstr (string option) throws axisfault
30 {
31 rpcserviceclient serviceclient = This. setoption ();
32
33 opaddentry = This. getqname (option );
34
35 string STR = (string) serviceclient. invokeblocking (opaddentry,
36 new object [] {}, new class [] {string. Class}) [0];
37 return STR;
38}
39 // returns a One-Dimensional String Array
40 Public String [] getarray (string option) throws axisfault
41 {
42 rpcserviceclient serviceclient = This. setoption ();
43
44 opaddentry = This. getqname (option );
45
46 string [] strarray = (string []) serviceclient. invokeblocking (opaddentry,
47 new object [] {}, new class [] {string []. Class}) [0];
48 return strarray;
49}
50 // return the instance of an object from WebService
51 public object GetObject (string option, object O) throws axisfault
52 {
53 rpcserviceclient serviceclient = This. setoption ();
54 QNAME = This. getqname (option );
55 object = serviceclient. invokeblocking (QNAME, new object [] {}, new class [] {o. getclass ()}) [0];
56 return object;
57}
58
59 /////////////////////////////////////// // readers can encapsulate data types by themselves, data types such as int, byte, and float
60}
61 client call method:
MZ. getwebservice. getwsbyaxis2 Ws = new Mz. getwebservice. getwsbyaxis2 ();
WS. wsurl = "http: // localhost: 8989/axis2/services/helloworld ";
Helloworld Hello = (helloworld) WS. GetObject ("getname", helloworld. Class );


System. Out. println (hello. getname ("zhangjin "));
2. Use Service. XML to publish WebService. This method is different from the pojo class directly in the pojo directory. To publish the myservice class to Web service, you need a service. xml file, which needs to be placed in the META-INF directory, the content of this file is as follows: <service name = "helloworld">
<Description>
Helloworld WebService
</Description>
<Parameter name = "serviceclass">
Service. helloworld
</Parameter>
<Messagereceivers>
<Messagereceiver MEP = "http://www.w3.org/2004/08/wsdl/in-out"
Class = "org. Apache. axis2.rpc. Receivers. rpcmessagereceiver"/>
<Messagereceiver MEP = "http://www.w3.org/2004/08/wsdl/in-only"
Class = "org. Apache. axis2.rpc. Receivers. rpcinonlymessagereceiver"/>
</Messagereceivers>
</Service>

The <service> element is used to publish a web service. A <service> element can only publish one WebService class. The name attribute indicates the WebService name. the following URL can obtain the WSDL content of the WebService:
Http: // localhost: 8080/axis2/services/myservice? WSDL
In addition, you can also create WebService operation methods in the file: You can use these service. XML files.
1 <service name = "helloworld">
2 <description>
3 helloworld Service
4 </description>
5 <parameter name = "serviceclass">
6 service. helloworld
7 </parameter>
8 <operation name = "getname">
9 <messagereceiver class = "org. Apache. axis2.rpc. Receivers. rpcmessagereceiver"/>
10 </Operation>
11 <operation name = "add">
12 <messagereceiver
13 class = "org. Apache. axis2.rpc. Receivers. rpcmessagereceiver"/>
14 </Operation>
15 </service>
16 to publish multiple WebServices, you can add <ServiceGroup> <service> </service>... <service> </ServiceGroup> to the file.

Related Article

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.