5 days Learn Jaxws-webservice programming the next day

Source: Internet
Author: User
Tags wsdl

Next day

Objective:

As you all know, just entering or returning a simple string, int does not make much sense in the actual work. Many times our service needs to return a data structure similar to list<person>,list<string>.

Let's see how it's going to work with JAXWS.

Goal:

1. Use WebService to invoke and return complex types of Java (such as:list<student> data)

First, write server side 1.1 to JAXWS return list type to do a simple POC

Before we formally return to our complex type, we first try to prove that JAXWS can return an object of complex type namely collection, so let's start by experimenting with Jaxws WebService and returning a list<string>.

Because, webservice except for simple types such as: int, string these objects, for complex type return, it uses the mechanism of serialize and deserialize.

That is, when transferring complex objects, WebService will serialize the complex type and deserialize the object when the client gets the server side back. So let's start with this little experiment to verify the JAXWS's serialize-deserialize ability.

The following is our server-side code:

package Ctsjavacoe.ws.fromjava;

import java.util.*;

import Javax.jws.WebMethod;

import Javax.jws.WebService;

@WebService

public class Collectionws {

    @WebMethod

    public list<string> Rtnmethod () {

       list<string> testlist = new arraylist<string> ();

       Testlist.add ("abc");

       testlist.add ("EFG");

       Testlist.add ("111");

       return testlist;

   }

}

Very simple, nothing much to say.

The service does not have input, and there is only one output, which is a list<string> type.

1.2 Compiling

All the detailed procedures generated by the WebService server side here are described in the "First day" tutorial.

1. Use Wsgen to compile the generated Java files, WSDL files and XSD files;

2. Copy the files from the compile-time output to the WSSRC directory to the SRC directory;

3. Modify the Sun-jaxws.xml file in the Webcontent\web-inf directory to add:

<endpoint name= ' COLLECTIONWS '

implementation= ' Ctsjavacoe.ws.fromjava.CollectionWS '

url-pattern= '/collectionwsservice '/>

4. Modify the Web. XML under the Webcontent\web-inf directory to join:

<servlet>

<servlet-name>CollectionWS</servlet-name>

<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>CollectionWS</servlet-name>

<url-pattern>/CollectionWSService</url-pattern>

</servlet-mapping>

5. Copy the files from the Jaxwsproject webcontent directory to the Tomcat Webapps\jaxwssample

Directory, and select all overrides;

6. Restart Tomcat;

7. Open an IE browser and enter:

HTTP://LOCALHOST:9090/JAXWSSAMPLE/COLLECTIONWSSERVICE?WSDL, you can see the following WSDL output.

First, write the client side 2.1 pre-compilation preparation

See the description in the first day tutorial for all the detailed procedures generated by the WebService client here.

1. Copy the server-side generated WSDL and XSD to the client project's WSDL directory

2. Since we continue to write asynchronous client calls using the polling method, we also need to open the Binding.xml file and change it:

<?xml version= "1.0" encoding= "UTF-8"?>

<bindings xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"

Xmlns:wsdl= "http://schemas.xmlsoap.org/wsdl/"

wsdllocation= "WSDL/COLLECTIONWSSERVICE.WSDL"

xmlns= "Http://java.sun.com/xml/ns/jaxws" >

<bindings node= "Wsdl:definitions" >

<enableAsyncMapping>true</enableAsyncMapping>

</bindings>

</bindings>

3. Use the Wsimport command to generate the "handle" required for client-side calls

4. Open the Collectionwsservice.java file in the generated handle, edit it, and place the two URLs url=... Change to the WSDL address of your server's actual webservice, which is the path to a local WSDL file by default

2.2 Writing test client calls the server side of WebService

Package Ctsjavacoe.ws.fromjava;

Import Javax.xml.ws.Response;

Import java.util.*;

public class Collectionwspollingclient {

public static void Main (string[] args) throws Exception {

Collectionwsservice service = new Collectionwsservice ();

COLLECTIONWS port = Service.getcollectionwsport ();

response<rtnmethodresponse> Rtnmethodasync = Port.rtnmethodasync ();

while (!rtnmethodasync.isdone ()) {

System.out.println ("is isn't done");

}

list<string> rtnlist = new arraylist<string> ();

try {

Rtnmethodresponse collectionresponse = Rtnmethodasync.get ();

Rtnlist = Collectionresponse.getreturn ();

System.out.println ("return size======" + rtnlist.size ());

for (String str:rtnlist) {

System.out.println ("output=====" + str);

}

} catch (Exception ex) {

Ex.printstacktrace ();

}

}

}

Especially when we type Collectionresponse.getreturn () in eclipse; This sentence when we come to see what happened:

As you can see, the JAXWS generated client has helped us do the "transformational" work, namely: what type of data we have on the server side, what type to upload to the client, and no need for us to go back to the complex type of Java based on XSD or related XML data. Jaxws is such a good thing!

Run the client and get the following output:

According to the output, is 3 records, these three records are the data returned by our server side, namely:

Testlist.add ("abc");

Testlist.add ("EFG");

Testlist.add ("111")

Third, the end of the next day

In the third day, we'll go into deep talk about returning a real complex type with JAXWS, namely: list<person> such objects to the client

5 days to learn Jaxws-webservice programming the next day

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.