WebService server and client development simple example

Source: Internet
Author: User
Tags soap wsdl

These days have been looking at WebService related knowledge.

WebService is a cross-platform access to third-party plug-ins, easy to use, multi-platform usage.

In development we may be the service side, provide WebService interface to others to access our system, or we may call other people's WebService interface to access other people's systems (such as querying the weather forecast).

The following is the development of the server and the client, we are only one of them in actual development.


Service-Side development:

① Create a service class, run the main method to publish it, the service side of the development is complete.


package com.lijianbo.service;import javax.jws.webmethod;import javax.jws.webparam;import  javax.jws.webresult;import javax.jws.webservice;import javax.xml.ws.endpoint;/** *  webservice *  mark the  Java  class as implementing  web service, or mark the  Java  interface as defined  Web  service  Interface  *  Service class This one, after writing the main method to publish the service, the service side of the development is complete.  *  */@WebService (servicename= "MyService", targetnamespace= "http://www.hello.com") public  class helloservice {         @WebMethod (operationName= " Aliassayhello ")      @WebResult (name=" Myreturn ")     public string  sayhello (@WebParam (name= "name")  string name) {         return   "Server,hello: "  + name;    }         public string saygoodbye (String name) {        return   "Server,goodbye: "  + name;     }         @WebMethod (exclude=true)//The current method is not published      public string sayhello2 (String name) {         return  "Hello"  + name;    }    public static  void main (String[] args)  {        /**          *  Parameter 1: Publishing address of the service           *  Parameter 2: Service implementation          *  Endpoint   Restarts a thread          */         endpoint.publish ("Http://172.18.100.52:456/hello",  new helloservice ());        &nbsp System.out.println ("****[ server ready ... WebService released successfully. ] ");     }}


② access to the browser, the URL is:

http://172.18.100.52:456/hello?wsdl

If you see the following results, you are successful.

This xml file does not appear to have any style information  associated with it. The document tree is shown below.<!--  published by jax-ws ri at http://jax-ws.dev.java.net. ri ' S version  is JAX-WS RI 2.2.4-b01. --><!-- generated by jax-ws ri  at http://jax-ws.dev.java.net. ri ' S version is jax-ws ri 2.2.4-b01.  --><definitions xmlns: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.hello.com" xmlns:xsd= "http ://www.w3.org/2001/XMLSchema " xmlns=" Http://schemas.xmlsoap.org/wsdl/" targetnamespace=" http://www.hello.com " name=" MyService "><types><xsd: Schema><xsd:import namespace= "http://www.hello.com"  schemalocation= "http://172.18.100.52:456/ Hello?xsd=1 "/></xsd:schema></types><message name=" Aliassayhello "><part name= "Parameters"  element= "Tns:aliassayhello"/></message><message name= "Aliassayhelloresponse "><part name=" Parameters " element=" Tns:aliassayhelloresponse "/></message><message  name= "Saygoodbye" ><part name= "parameters"  element= "Tns:saygoodbye"/></message> <message name= "Saygoodbyeresponse" ><part name= "parameters"  element= "TNS: Saygoodbyeresponse "/></message><porttype name=" HelloService "><operation name=" Aliassayhello "><input wsam:action=" Http://www.hello.com/HelloService/AliassayHelloRequest "  Message= "Tns:aliassayhello"/>< output wsam:action= "Http://www.hello.com/HelloService/AliassayHelloResponse"  message= "TNS: Aliassayhelloresponse "/></operation><operation name=" Saygoodbye "><input wsam: action= "Http://www.hello.com/HelloService/sayGoodbyeRequest"  message= "Tns:saygoodbye"/><output  wsam:action= "Http://www.hello.com/HelloService/sayGoodbyeResponse"  message= "Tns:saygoodbyeresponse" /></operation></porttype><binding name= "helloserviceportbinding"  type= "TNS: HelloService "><soap:binding transport=" http://schemas.xmlsoap.org/soap/http " style=" Document "/ ><operation name= "Aliassayhello" ><soap:operation soapaction= ""/><input><soap : body use= "literal"/></input><output><soap:body use= "literal"/></output> </operation><operation name= "Saygoodbye" ><soap:operation soapaction= ""/><input ><soap:body use= "literal"/&Gt;</input><output><soap:body use= "literal"/></output></operation></ Binding><service name= "MyService" ><port name= "Helloserviceport"  binding= "TNS: Helloserviceportbinding "><soap:address location=" Http://172.18.100.52:456/hello "/></port> </service></definitions>


Client development:

①cmd into the command line.

Input:

wsimport-d C:-keep-verbose http://172.18.100.52:456/hello?wsdl

It has several very important parameters,

-D indicates the output directory, the directory must exist beforehand, or the export fails.

-keep Indicates whether the source code is also exported when exporting the WebService class file Java file.

The-verbose represents the detailed information.

Look at our export command. We are directly in the C drive.

Show:

c:\>wsimport-d c:-keep-verbose http://172.18.100.52:456/hello?wsdlparsing WSDL ... Generating code...com\hello\aliassayhello.javacom\hello\aliassayhelloresponse.javacom\hello\ Helloservice.javacom\hello\myservice.javacom\hello\objectfactory.javacom\hello\saygoodbye.javacom\hello\ Saygoodbyeresponse.javacom\hello\package-info.java

This allows the client's code to be generated. We are generating a copy of the generated code into the project under the C drive.


② run the test class, call the WebService interface

package com.lijianbo.client;import java.rmi.remoteexception;import  javax.xml.rpc.serviceexception;import com.hello.helloservice;import com.hello.myservice;public  class testhello { /**     *  through wsimport  Parsing WSDL generating client code call WebService service      *      *  Run the test class to successfully invoke the server's interface and get the returned data.      *      */    public  Static void main (String[] args)  throws serviceexception, remoteexception {         /**         *  <service name= "MyService" >         *  Get Service Name          */         Myservice mywebservice = new myservice ();                /**          * <port name= "Helloserviceport"  binding= "TNS : Helloserviceportbinding ">         */        helloservice hs = mywebservice.gethelloserviceport ();         system.out.println ("HS:" +HS);        /**          *  Calling Methods           */        system.out.println (Hs.saygoodbye ("sjk============="));         system.out.println (Hs.aliassayhello ("sjk================"));     }}


The result of the operation is:

Hs:jax-ws RI 2.2.4-b01:stub for Http://172.18.100.52:456/hello server, goodbye:sjk============= server, hello:sjk=========== =====


As can be seen here, the server's interface was successfully accessed, and the data was returned. Description The call WebService succeeded.


WebService basic knowledge point is this, when the actual service-side code is published in the server, rather than the native.

Client code can also be used to invoke a generated jar package (server-side generation).

In the actual development, the client often also stitching parameters according to the soap document, passing the parameters through the SOAP protocol, then using the client proxy class to invoke the interface and get the returned data.


This article from "Jianbo" blog, reproduced please contact the author!

WebService server and client development simple example

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.