Web Service that little Thing (1)

Source: Internet
Author: User
Tags soap jboss wsdl

The Web service, or "Web Services", abbreviated to WS, literally means "web-based services." But the service is both sides, has the service demand side, has the service provider. The service provider publishes the service, and the service demand party invokes the service published by the service provider. In fact, that's all, there's not much on the tall stuff.

This article will describe the tools for using Java to develop WS and how to use it, from an actual combat perspective.

If you're more professional, WS is actually a tool for communicating with heterogeneous systems on the HTTP protocol. That's right! WS is still based on the HTTP protocol, that is, the data is transmitted over HTTP.

Since the WS, communication between heterogeneous systems is no longer a distant dream. For example, a Java system can be called in a PHP system to release WS, get data from a Java system, or push data into a 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

The topic I want to share with you today is how to publish and call WS in Java? I hope this article can help you!

1. Using the JDK to publish WS

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

package demo.ws.soap_jdk;import javax.jws.WebService;@WebServicepublic interface HelloService { String say(String name);}

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

The second step: implement this WS interface, complete the specific business logic in the implementation class, for the sake of simplicity, let's write a Hello world meaning.

 package demo.ws.soap_jdk; Import Javax.jws.WebService;  @WebService (ServiceName =  "HelloService", PortName =  "Helloserviceport", Endpointinterface = public class helloserviceimpl implements HelloService { public String sayreturn  "hello" + Name;}}   

Step three: Write a Server class that is used to publish 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/hello"; HelloService helloService = new HelloServiceImpl(); Endpoint.publish(address, helloService); System.out.println("ws is published"); }}

Just use the JDK provided to javax.xml.ws.Endpoint Publish WS, just provide a WS address, and a service instance (HelloService) is required.

Now you can run the main method of the Server class, you will see "WS is published" Prompt in the console, congratulations, WS has been successfully released!

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

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

Note: There is a suffix at the end of the address ?wsdl , but 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 the WS service you are publishing can now be used by someone else.

2. Calling WS via Client

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

There is a bin directory under the JDK installation directory, which contains a large number of command-line tools that Path can be used on the command console by using the commands provided by the JDK as long as your environment variables point to that path.

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

wsimport http://localhost:8080/ws/soap/hello?wsdljar -cf client.jar .rmdir /s/q demo

The above three-line commands are interpreted as follows:

    • First line: Generating a class file from a WSDL address
    • Second line: Compress several class files into a jar package with the jar command
    • Third line: Delete the generated class file (delete the root directory)

Eventually you will get a jar package named, which client.jar configures the jar package into your classpath to make it easy to use the classes in the code below.

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

Step two: Write a client class that calls WS and needs to use the WS-Client jar package generated in the previous step.

Client {    public static void main(String[] args) {        HelloService_Service service = new HelloService_Service(); HelloService helloService = service.getHelloServicePort(); String result = helloService.say("world"); System.out.println(result); }}

The above code is a little bit weird, which HelloService_Service is the jar in the class, it can be understood as WS factory class, through it can generate a specific WS interface, for example, call service.getHelloServicePort() method, get an HelloService instance, it is through this instance to invoke the method.

Run the main method of the Client class and you will see the result you expect, "Hello World," and try it yourself.

This is a typical "proxy mode" scenario where you actually call WS for a proxy object, and this is a "static proxy," Let's talk about how to invoke WS with Dynamic proxy.

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

Package demo.ws.soap_jdk;Import java.net.URL;Import Javax.xml.namespace.QName;Import javax.xml.ws.Service;public classdynamicclient {publicStaticvoid Main (String[] (args) {try {URL wsdl = newURL ("HTTP://LOCALHOST:8080/WS/SOAP/HELLO?WSDL");QName serviceName = newqname ( "http://soap_jdk.ws.demo/", 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 (           

At this point, you just need to provide a HelloService interface locally, without Client.jar, directly to the WSDL programming, but you have to define separately serviceName with portName these two things, and finally to invoke the JDK provided by the javax.xml.ws.Service class build service Object, which is also a factory object that gets the instance we need through the factory object HelloService . It seems that this way is 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 the JDK to publish WS, but you can also use the JDK to call WS, all of which is simple and natural. However, it is important to note that this feature is only available from JDK 6, and if you are still using JDK 5 or lower, then unfortunately you have to use the following tools to publish and call WS, respectively:

    • JAX-WS ri:https://jax-ws.java.net/
    • axis:http://axis.apache.org/
    • cxf:http://cxf.apache.org/

Of course, the tools for publishing and calling WS are more than just these, but they are the best WS open source projects in the Java world.

WS in this article is actually a Java specification, called JAX-WS (JSR-224), the full name of the Java API for xml-based Web Services, you can interpret the specification as an officially defined series of interfaces.

JAX-WS has an official implementation, which is the Jax-ws RI mentioned above, which is an implementation provided by Oracle, and the same specification is implemented by Apache's Axis and CXF. Axis is relatively older, and CXF's previous life is XFire, a well-known WS-framework that specializes in integrating with Spring.

In essence, Jax-WS is SOAP-based, and the full name of the SOAP is simple Object access Protocol (Easy objects), although the name with the word "simple", in fact, is not simple, do not believe you can Baidu a bit.

In order to make WS development and use more simple and lightweight, there is another style of WS, called JAX-RS (JSR-339), the full name of the Java API for RESTful Web Services, is also a specification, there are also several implementations, respectively:

    • jersey:https://jersey.java.net/
    • restlet:http://restlet.com/
    • resteasy:http://resteasy.jboss.org/
    • cxf:http://cxf.apache.org/

Among them, Jersey is Oracle's official implementation, Restlet is the most established implementation, Resteasy is the implementation provided by JBoss, CXF is the implementation provided by Apache (described above).

As you can see, CXF not only for the development of SOAP-based WS, but also for the development of REST-based WS, how could we miss such a good framework?

How do I use CXF to simplify our WS-development? We'll see you next time!

Web Service that little Thing (1)

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.