Design, development and application of the webservcie client in java

Source: Internet
Author: User

Welcome to: http://observer.blog.51cto.com

Webservcie is a technology that supports cross-language cross-platform development. servers can be built in various computer languages, and clients can also be developed in various computer languages. If a server is built in java, C ++, or php, other languages can develop the client based on its open wsdl, then, call the method just like calling the method of the local project. This article uses java to develop the client. The example relies on the previous article: CXF to build a webService server. If you do not understand, read this article first and then read this article.
1. Download The CXF package
The client in this article uses CXF built-in tool development, if too lazy to find the Internet can be downloaded here: apache-cxf-2.6.1.tar

, Decompress the development kit.

2. Configure Environment Variables
This step can be ignored. However, if you do not configure environment variables, you must go to the bin directory of cxf to call the command, which lacks flexibility. Therefore, this article will configure it.
Configuration in liunx:
Call the console and enter the following command:

~ $ Gedit ~ /. Bashrc
Add:
Export CXF_HOME =/home/roadahead/apache-cxf-2.6.1
Export CLASSPATH = $ CLASSPATH: $ {CXF_HOME}/lib
Export PATH = $ PATH: $ {CXF_HOME}/bin
Map "/home/roadahead/apache-cxf-2.6.1" to your CXF directory.

Save and exit. Call the following command to make it take effect immediately:
~ $ Source ~ /. Bashrc

Configuration in windows:
Right-click "my computer" --> "property" --> "advanced" --> "environment variable"
Create an environment variable:
Variable name: CXF_HOME
Variable value: C: \ apache-cxf-2.6.1
The variable value is your CXF directory.
Add:; % CXF_HOME % \ lib to CLASSPATH
Add:; % CXF_HOME % \ bin in PATH

Map C: \ apache-cxf-2.6.1 to your CXF directory.

OK.

Third: Call the command wsdl2java
Deploy the server. If the server is not deployed, call the following command to bring up the console:

~ $ Wsdl2java-d/home/roadahead/workspace/webServiceClient/src http: // 127.0.0.1: 8080/webServiceCXF/services/Service? Wsdl
Here-d is followed by the location of the generated client code, which is placed in the src directory under my project webServiceClient. The endpoint is the URL of your Web Service wsdl. in the previous article, click {http://service.cxf.observer.com/#gradeserviceto find the address. In actual project development, you must replace 127.0.0.1: 8080 with the address of your website domain name to generate a client, because the client is called remotely, while 127.0.0.1: 8080 is only applicable to the local machine. Because it is only used for local testing, 127.0.0.1: 8080 is used.
If no error occurs, you will see a new directory under your directory, and you will see a variety of new directories. java file. That's right. These are the webservice Clients generated by using the wsdl2java command.
Fourth: simple use of the Client
After the client is generated, put it in eclipse and you will see errors such:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11313L501-0.png "title =" webservcie client 1 "/>

It doesn't matter. We just need to make the mistake, for example:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11313M350-1.png "title =" webservcieclient 2.png "/>

After removing the error, deploy and run the server, and then call the client to link to webservice. The call method is as follows:

public static void main(String[] args) {    GradeService_Service gradeService_Service = new GradeService_Service();    GradeService gs = gradeService_Service.getGradeServicePort();    String gstr = gs.getGradeName(12120);    System.out.println(gstr);}

When the output is succeed, the call is successful.
At this point, the webservice client has been developed and can basically meet the work needs.

5. encapsulate the client
Developing a webservice client (hereinafter referred to as a client) is very simple. If there is no encapsulation, I certainly don't want to say anything.
Why is it encapsulated?

I,If You encapsulate your client and package it into a jar package, which java program will directly use the jar package if you want to use it? Is this a great deal?

II,The Client involves various unexpected feedback. If the server is not running, how does the client provide feedback? If the server is shut down while the client is running, how can we provide feedback? In addition, the feedback methods of various program calling clients are different. How can we encapsulate various Program Calling methods? For example, the web feedback mode is jsp, the swing feedback mode is JOptionPane. showMessageDialog, And the android feedback mode can be Toast. makeText.

III,Clients without encapsulation are not required to capture exceptions during calls. If you call the client directly, when the server is closed, if the web browser is used, an error message is displayed, which is obviously unfriendly and makes it easy for programmers to see unknown errors.
This document uses"Similar to singleton" + "similar to factory mode" + "Exception Handling"To implement encapsulation. This is similar because there is a static link class in the encapsulation class instead of a real Singleton, the reason is that the original client code is separated from the encapsulation class, so that when the wsdl changes, you can call the wsdl2java command again to generate the code without changing anything else. The same is true for the factory model. If the client programmer has to call the original client code, it can also be used flexibly.
First, create an exception handling class. My class is as follows:

package com.observer.service.manager;public class ConnectException extends Exception {    private String title;    private String details;    public ConnectException(String title, String details) {        super();        this.title = title;        this.details = details;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getDetails() {        return details;    }    public void setDetails(String details) {        this.details = details;    }    @Override    public String toString() {        return "com.observer.service.ConnectException [title=" + title + ", details=" + details                + "]";    }}

Then the serviceManager class, such as my encapsulation class: GradeServiceManager is as follows:

Import javax. xml. ws. webServiceException; import com. observer. service. gradeService; import com. observer. service. gradeService_Service; public class GradeServiceManager {private static GradeService_Service gradeService_Service = null; private static GradeService createGradeService () throws ConnectException {try {synchronized (Thread. currentThread () {if (gradeService_Service = null) {gradeService_Service = new GradeService_Service () ;}} catch (WebServiceException e) {e. printStackTrace (); throw new ConnectException ("connection exception 1", "the server is being maintained. Please try again later, or contact the server maintenance personnel for processing ");} return gradeService_Service.getGradeServicePort ();} public static String getGradeName (long toid) throws ConnectException {String gradeName = null; try {GradeService gs = createGradeService (); gradeName = gs. getGradeName (toid);} catch (ConnectException e) {e. printStackTrace (); throw e;} catch (Exception e) {e. printStackTrace (); throw new ConnectException ("connection exception 2", "the server is being maintained. Please try again later, or contact the server maintenance personnel for processing");} return gradeName ;}}


Here, my webservice only has one getGradeName method, so we simply put createGradeService together. If there are more methods in actual application, you can encapsulate the createGradeService method and the webservcie Method for flexible use.
6. Export the jar package and import it into the jar package
Export jar packageRight-click the project --> Export --> JAR file and select the saved path and name. Here I select webServiceClient. jar and Finish.
Import the jar package: A common project is the same as importing it to the ssh framework. Here we will talk about importing the jar package to maven.
Maven imports the jar package. I personally think there are two ways to make it easier. One is to put the jar package into the maven library, and then configure the pom in the normal way. xml, the other is to put the jar package in the directory under the project and then in the pom. in xml. Because the jar package here is not large, and for distributed development, everyone must configure and put the jar package in their maven library, so the next method is used here.
1. Copy webServiceClient. jar to the src/main/webapp/WEB-INF/lib directory under the project. If there is no lib directory, create one by yourself.
2. Add the following configuration to the <dependencies> label in pom. xml:

<dependency><groupId>webServiceClient</groupId><artifactId>webServiceClient</artifactId><version>1.0</version><scope>system</scope><systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/webServiceClient.jar</systemPath></dependency>

Here, <groupId>, <artifactId>, and webServiceClient. jar are all your jar package file names.
7. Application Client
Now, we have completed the configuration. Next we will have a sense of accomplishment. We will apply the client in servlet, swing, and android.
Servlet can be used directly like this:

String gradename = null;try {    gradename = GradeServiceManager.getGradeName(2323);} catch (ConnectException e) {    e.printStackTrace();    request.setAttribute("massage", e.getDetails());    request.getRequestDispatcher("/errer.jsp").forward(request, response);    return;}

In swing, we can use the following:

String gradename = null;try {    gradename = GradeServiceManager.getGradeName(2323);} catch (ConnectException e) {    e.printStackTrace();    JOptionPane.showMessageDialog(this, e.getDetails(), e.getTitle(), JOptionPane.ERROR_MESSAGE);    return;}

In android, we can use:

String gradename = null;try {    gradename = GradeServiceManager.getGradeName(2323);} catch (ConnectException e) {    e.printStackTrace();    Toast.makeText(XXXXX.this, e.getDetails(), Toast.LENGTH_LONG).show();    return;}

Of course, swing, android, or web still have a lot of usage options!

Share my client project (webServiceClient) and maven web Project (serviceServletClient) code here: http://down.51cto.com/data/857036

Next, we will design, develop, and apply the webservice client of js.

This article is from the "Observer * unrestrained" blog, please be sure to keep this source http://observer.blog.51cto.com/4267416/1238972

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.