The Web Service thing.

Source: Internet
Author: User
Tags soap webservice annotation jboss wsdl
Web Service that Thing (1)Original address: http://my.oschina.net/huangyong/blog/286155

directory [-] 1. Use JDK to publish WS 2. Call WS 3 through the client. Summarize

Web service, "Web Services", abbreviated as WS, literally, it's actually "web-based services." But the service is both sides, has the service demand side, has the service provider side. The service provider publishes the service, and the service demand party invokes the service published by the service provider. In fact, there are not many tall things.

This article will describe the tools for using Java to develop WS and how to use it in terms of actual combat.

If you're more professional, WS is actually a tool for implementing heterogeneous system communication on HTTP protocols. That's right. WS is also based on the HTTP protocol, which means that the data is transmitted over HTTP.

Since the WS, communication between heterogeneous systems is no longer an unreachable dream. For example, you can invoke the Java system's externally released WS in the PHP system, get the data from the Java system, or push the data to the Java system.

If you want to learn more about WS concepts and terminology, you can look at the following Baidu Encyclopedia:

Http://baike.baidu.com/view/67105.htm

Today I want to share with you the topic of how to publish and invoke WS in Java. I hope this article will help you. 1. Use JDK to publish WS

First step: The first thing you need to do is to write a service interface.

Package demo.ws.soap_jdk;

Import Javax.jws.WebService;

@WebService Public
interface HelloService {

    string Say (string name);
}

Put a WebService annotation on the interface, stating that the interface is a WS interface (called "Endpoint, Endpoint"), where the method is the WS method (called "Operation, Operation").

Step two: Implement this WS interface, complete the specific business logic in the implementation class, and in order to be simple, let's write a Hello world meaning.

Package demo.ws.soap_jdk;

Import Javax.jws.WebService;

@WebService (
    serviceName = "HelloService",
    portname = "Helloserviceport",
    endpointinterface = " Demo.ws.soap_jdk. HelloService "
) public
class Helloserviceimpl implements HelloService {public

    string say (string name) { return
        "Hello" + name;
    }

Step three: Write a Server class for publishing WS, which can be implemented directly using the tools provided by the JDK.

Package demo.ws.soap_jdk;

Import Javax.xml.ws.Endpoint;

public class Server {public

    static void Main (string[] args) {
        String address = http://localhost:8080/ws/soap/he Llo ";
        HelloService HelloService = new Helloserviceimpl ();

        Endpoint.publish (address, helloservice);
        System.out.println ("WS is published");
    }

You can publish ws simply by using the javax.xml.ws.Endpoint provided by the JDK, providing a WS address and a service instance (HelloService).

Now that you can run the main method of the Server class and see the "WS is published" Prompt in the console, congratulations, WS has been published successfully.

Fourth Step: Open your browser, enter the following address in the Address bar:

http://localhost:8080/ws/soap/hello?wsdl

Note: There is a WSDL suffix at the end of the address, and there is no suffix in the addresses in the Server class. At this point, you will see the following XML document in your browser:

When you see this WSDL document, it means that your published WS service can now be used by someone else. 2. Invoke WS via client

First step: Use the command-line tools provided by JDK to generate the WS client jar package.

The JDK installation directory has a bin directory that contains a large number of command-line tools, so long as your PATH environment variable points to that path, you can use the relevant commands provided by JDK on the command console.

Among them, there is a command-line tool called Wsimport, which is used to generate WS client code through WSDL, and you only need to enter the following command:

Wsimport http://localhost:8080/ws/soap/hello?wsdl
jar-cf Client.jar.
rmdir/s/q Demo

The above three lines are interpreted as follows: The first line: Generating a class file from a WSDL address the second line: compressing several class files into a jar package with the jar command third row: Delete the generated class file (delete the root directory)

Eventually you will get a jar package named Client.jar, which configures the jar package into your classpath to facilitate the use of the classes in the following code.

Tip: You can put the above three line commands in a bat file and double-click in Windows to run.

Step two: Write a client class to invoke WS, which requires the WS client jar package that was generated in the previous step.

Package demo.ws.soap_jdk;

public class Client {public

    static void Main (string[] args) {
        Helloservice_service Service = new Helloservice_ser Vice ();

        HelloService HelloService = Service.gethelloserviceport ();
        String result = Helloservice.say (' world ');
        SYSTEM.OUT.PRINTLN (result);
    }

The above code is a bit weird, where Helloservice_service is a class in a jar package that can be interpreted as WS's factory class, through which you can generate specific WS interfaces, such as invoking the Service.gethelloserviceport () method, A HelloService instance is obtained, and it is through this instance that the method is invoked.

Run the main method of the Client class, and you'll see the results you expect, "Hello World," and try it yourself.

Visible, this is a typical "agent mode" scenario where you actually call WS on the proxy object, and this is a "static proxy," and let's talk about how to invoke WS using the "dynamic proxy" approach.

In fact, JDK already has the function of dynamic proxy, for WS, the JDK also provides a good tool, like the following code:

 package DEMO.WS.SOAP_

Jdk
Import Java.net.URL;
Import Javax.xml.namespace.QName;

Import Javax.xml.ws.Service; public class Dynamicclient {public static void main (string[] args) {try {URL wsdl = new URL ("H
            Ttp://localhost:8080/ws/soap/hello?wsdl ");
            QName serviceName = new QName ("http://soap_jdk.ws.demo/", "HelloService");
            QName portname = new QName ("http://soap_jdk.ws.demo/", "Helloserviceport");

            Service service = service.create (WSDL, serviceName);
            HelloService HelloService = Service.getport (PortName, Helloservice.class);
            String result = Helloservice.say (' World ');
        SYSTEM.OUT.PRINTLN (result);
        catch (Exception e) {e.printstacktrace (); }
    }
}

At this point, just provide a helloservice interface locally, without Client.jar, directly to WSDL programming, but you need to define the ServiceName and portname of these two things, the last to invoke JDK provided Javax.xml . ws. Service class generates the service object, which is also a factory object that obtains the HelloService instance we need through the factory object. Seemingly this way is also not particularly dynamic, after all, HelloService interface still need to provide their own. 3. Summary

With this article, you can see that not only can you use JDK to publish WS, but you can use JDK to invoke WS, which is simple and natural. Note, however, that this feature is available from JDK 6, and if you're still using JDK 5 or lower, it's a shame you have to use the following tools to publish and Invoke WS, which are: Jax-ws ri:https://jax-ws.java.net/ axis:http://axis.apache.org/cxf:http://cxf.apache.org/

Of course, the tools to publish and invoke WS are more than just these, but they are the best WS open source projects in the Java world.

The WS described in this article is actually a Java specification, named Jax-ws (JSR-224), a full name Java API for xml-based Web Services that can interpret specifications as an officially defined set of interfaces.

JAX-WS has an official implementation, the aforementioned Jax-ws RI, which is an Oracle-provided implementation, and Apache's Axis and CXF also implement the specification. Axis is relatively older, and CXF's previous incarnation is XFire, a well-known WS framework that specializes in integrating with Spring.

Essentially, JAX-WS is soap based, and the full name of soap is the Simple Object access Protocol, although there is a "simple" word in it, it is not simple, do not believe you can Baidu.

To make WS development and use easier and lighter, there was another style of WS named Jax-rs (JSR-339), the full Java API for RESTful Web Services, also a specification and several implementations, They were: jersey:https://jersey.java.net/restlet:http://restlet.com/resteasy:http://resteasy.jboss.org/cxf:http:// cxf.apache.org/

Of these, Jersey is an Oracle-provided implementation, Restlet is the oldest implementation, Resteasy is the implementation provided by JBoss, CXF is the implementation that Apache provides (described above).

It is obvious that CXF is not only used to develop SOAP based WS, but also to develop a REST based WS, so how can we miss this good framework.

How to use CXF to simplify our WS development. We'll see you next time.

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.