5 days Learn Jaxws-webservice programming the third day

Source: Internet
Author: User
Tags wsdl

Objective:

In the second day of study, we learned how JAXWS returns a relatively complex Java data type of list<string> type to the client, and today we will take a step further into making a real Java complex type of WebService transfer call.

Goal:

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

First, write server side 1.1 make Java Bean-person object

This time we will return a list<person> type to the client.

To make our person class first, the code is as follows:

Package Ctsjavacoe.ws.fromjava.bean;

Import java.io.*;

public class Person implements Serializable {

Private String name = "";

private int age = 0;

Private String gender = "";

Public String GetName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

public int getage () {

return age;

}

public void Setage (int.) {

This.age = age;

}

Public String Getgender () {

return gender;

}

public void Setgender (String gender) {

This.gender = gender;

}

}

1.2 Making the service side

Package Ctsjavacoe.ws.fromjava;

Import java.util.ArrayList;

Import java.util.List;

Import ctsjavacoe.ws.fromjava.bean.*;

Import Javax.jws.WebMethod;

Import Javax.jws.WebService;

@WebService

public class Javacomplextype {

@WebMethod

Public list<person> Getperson () {

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

Person p = new person ();

P.setname ("abc");

P.setage (31);

P.setgender ("female");

Testlist.add (P);

p = new person ();

P.setname ("Def");

P.setage (33);

P.setgender ("male");

Testlist.add (P);

p = new person ();

P.setname ("AAA");

P.setage (26);

P.setgender ("female");

Testlist.add (P);

return testlist;

}

}

The service does not have input, there is only one output, and the output is a list<person> type, which returns a list to the client, which has three person structure data

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= ' Javacomplextype '

implementation= ' Ctsjavacoe.ws.fromjava.JavaComplexType '

url-pattern= '/javacomplextypeservice '/>

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

<servlet>

<servlet-name>JavaComplexType</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>JavaComplexType</servlet-name>

<url-pattern>/JavaComplexTypeService</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/JAVACOMPLEXTYPESERVICE?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. Copy the Ctsjavacoe.ws.fromjava.bean.Person class to the corresponding SRC directory of the client project, because in the second day we are using List<string> The string in the XSD corresponding to the string is a basic type of webservice and therefore does not need to be re-styled on the client.

And this time we return is a list<person>, this person class is not the XSD itself has the data type, so when the client gets WebService return, need to be in the client to shape the person, While modeling needs to have an object to tell the client what type I created, so this side than the next day of the tutorial more than one step, that is, manually copy the person class to the client project .

3. 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/JAVACOMPLEXTYPESERVICE.WSDL"

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

<bindings node= "Wsdl:definitions" >

<enableAsyncMapping>true</enableAsyncMapping>

</bindings>

</bindings>

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

5. Open the Javacomplextypeservice.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.*;

Import ctsjavacoe.ws.fromjava.bean.*;

public class Javacomplextypepollingclient {

public static void Main (string[] args) {

Javacomplextypeservice service = new Javacomplextypeservice ();

Javacomplextype port = Service.getjavacomplextypeport ();

response<getpersonresponse> Getpersonasync = Port.getpersonasync ();

while (!getpersonasync.isdone ()) {

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

}

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

try {

Getpersonresponse getpersonresponse = Getpersonasync.get ();

Rtnlist = Getpersonresponse.getreturn ();

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

for (person P:rtnlist) {

System.out.println ("person=====" + p.getname () + ""

+ p.getage () + "" + P.getgender ());

}

} catch (Exception ex) {

Ex.printstacktrace ();

}

}

}

When we type Getpersonresponse.getreturn ()in eclipse:

We can see that JAXWS has helped us transform list<person>, and we just need to use the corresponding generics:

List<person>rtnlist = new arraylist<person> () go and pick it up.

Run the client and get the following output:

Iii. end of the third day

Through the second day, the third day of study, we have basically mastered the JAXWS general application, for the Java complex type of transmission has a comprehensive understanding.

In the next two days of study, we will use the Jaxws mtom feature, with WebService to transfer binary files, such as: Jpg/gif pictures

5 days Learn Jaxws-webservice programming the third 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.