Using the webservice that comes with the JDK

Source: Internet
Author: User

WebService is a good thing, not much to say, neat

Server-side

Look at the server-side structure:

First, define an interface for exposing:

Package com.abc.webservice;/** * Externally exposed interface. */public interface Iwebservice {public string hello (String who);

Then define the implementation class for this interface:

Package Com.abc.webservice.impl;import javax.jws.webservice;import com.abc.webservice.iwebservice;/** * wsdl:            Porttype:myservice * Wsdl:service:MyWebService */@WebService (name= "MyService", servicename= "MyWebService", Targetnamespace= "http://www.abc.com") public class Webserviceimpl implements Iwebservice {@Override public string Hello (string who) {return ' hello ' + who + '! ';}}

Note the name here, which represents

The name of the Web Service. Used as the name of the Wsdl:porttype when mapped to WSDL 1.1.

ServiceName, it says

The service name of the Web service. Used as the name of the Wsdl:service when mapped to WSDL 1.1.

TargetNamespace is the package name of the code that you generated for the Java client, and the generated package name is automatically reversed, such as www.abc.com, which is the package com.abc.*.

Finally, publish the WebService:

Package Com.abc.webservice;import Javax.xml.ws.endpoint;import com.abc.webservice.impl.webserviceimpl;/** * Publish WebService */public class Publisher {public static void main (string[] args) {System.out.println ("Start publi        SH service ");        Endpoint.publish ("Http://localhost:8080/MyService", New Webserviceimpl ());    System.out.println ("End Publish Service"); }}

After that, you can open the browser and enter the Url:http://localhost:8080/myservice you just released to see the effect:

Click on the hyperlink to see the generated WSDL, the following is the generated WSDL:

<?xml version= "1.0"  encoding= "UTF-8"? ><definitionsxmlns:wsu= "Http://docs.oasis-open.org/wss /2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd "xmlns:wsp=" Http://www.w3.org/ns/ws-policy " xmlns: Wsp1_2= "Http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam= "Http://www.w3.org/2007/05/addressing/metadata " xmlns:soap=" http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns=" http://www.abc.com " xmlns:xsd=" http:/ /www.w3.org/2001/xmlschema "xmlns=" http://schemas.xmlsoap.org/wsdl/" targetnamespace=" http://www.abc.com " Name= "MyWebService" ><types><xsd:schema><xsd:import namespace= "http://www.abc.com" schemalocation= "Http://localhost:8080/MyService?xsd=1"  /></xsd:schema></types><message  name= "Hello" ><part name= "parameters"  element= "Tns:hello"  /></message>< Message name= "Helloresponse" ><part name= "parameters"  element= "Tns:helloresponse"  / ></message><Porttype name= "MyService" ><operation name= "Hello" ><input wsam:action= "/HTTP/ Www.abc.com/MyService/helloRequest "message=" Tns:hello " /><output wsam:action="/HTTP/ Www.abc.com/MyService/helloResponse "message=" Tns:helloresponse " /></operation></portType> <binding name= "myserviceportbinding"  type= "Tns:myservice" ><soap:binding transport= " Http://schemas.xmlsoap.org/soap/http "style=" document " /><operation name=" Hello "><soap:o peration soapaction= ""  /><input><soap:body use= "literal"  /></input> <output><soap:body use= "literal"  /></output></operation></binding>< Service name= "MyWebService" ><port name= "Myserviceport"  binding= "tns:MyServicePortBinding" ><soap:address location= "Http://localhost:8080/MyService"  /></port></service> </definitions>

Do not understand it's okay, this is the WSDL, belongs to another category, need to know the friend can go to search the relevant information. This is just to show how to use the webservice that comes with the JDK.

Java Client

Of course, webservices can be called by a Java client, or it can be called by a program other than the Java language, where we only see how the Java client is invoked.

Create a new poject to impersonate the client on another machine and open the command line:

>CD D:\WORKSPACE\WEBSERVICECLIENT\SRC

Use the Wsimport command from the JDK to generate the Java client (note that there is a point in the middle that represents the current directory):

>wsimport-keep. http://localhost:8080/MyService?wsdl

This statement indicates that the generated client code is saved under the current folder.

The client code for the following structure is generated (the selected part of the figure, the WebService package was built by itself), and just mentioned that the generated Java client code will be placed under the COM.ABC package :

As for what is in the generated classes, you go and see for yourselves.

Then write the client code (COM.ABC.WEBSERVICE.WEBSERVICECLIENT.JAVA):

Package Com.abc.webservice;import Com.abc.mywebservice;public class WebServiceClient {public static void main (string[]        args) {MyWebService MyWebService = new MyWebService ();        Note the following sentence myservice MyService = Mywebservice.getmyservieport ();    System.out.println (Myservice.hello ("Alvis")); }}

The MyWebService class here is the class generated by the Wsimport command based on the WebService WSDL. Here is a paragraph in the WSDL:

<service name= "MyWebService" ><port name= "Myserviceport" binding= "tns:myserviceportbinding" ><soap: Address location= "Http://localhost:8080/MyService"/></port></service>

As can be seen from the WSDL, there is a <service> name of MyWebService, which contains a <port>

Mywebservice.getmyserviceport ();

In fact, this sentence gets the example of the MyService class, which is actually the proxy object of the remote WebService implementation class. You can look at the definition of this method in the generated MyWebService class:

@WebEndpoint (name = "Myserviceport") public MyService Getmyserviceport () {return super.getport (new QName ("HTTP://WWW.A Bc.com "," Myserviceport "), Myservice.class);}

Once you have the instance of this myservice, you can use that instance to invoke the remote End method:

Myservice.hello ("Alvis")

Let's look at what's in the MyService class:

package com.abc;import javax.jws.webmethod;import javax.jws.webparam;import  Javax.jws.webresult;import javax.jws.webservice;import javax.xml.bind.annotation.xmlseealso;import  javax.xml.ws.action;import javax.xml.ws.requestwrapper;import javax.xml.ws.responsewrapper;/**  * this class was generated by the jax-ws ri. * jax-ws  ri 2.2.4-b01 * generated source version: 2.2 */@WebService (name  =  "MyService", targetnamespace =  "http://www.abc.com") @XmlSeeAlso ({     objectfactory.class}) public interface myservice {    /**      *  @param  arg0     *  @return       *     returns java.lang.String     */      @WebMethod      @WebResult (targetnamespace =  ")      @RequestWrapper (localname =   "Hello",         targetnamespace =  "/http" Www.abc.com ", classname = " Com.abc.Hello ")      @ResponseWrapper (localname  =  "Helloresponse",          targetnamespace =   "http://www.abc.com", classname =  "Com.abc.HelloResponse")      @Action (input =  "Http://www.abc.com/MyService/helloRequest",          output =  "Http://www.abc.com/MyService/helloResponse")     public  string hello (         @WebParam (name =  "arg0",  targetNamespace =  "")         string arg0);}

As you can see, MyService is an interface because the real implementation is at the far end. In fact, there is a method, that is, we defined in the remote hello.

To run the client code:


here is the project source code, for the needs of friends reference.

Using the webservice that comes with the JDK

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.