Java WebService interface Generation and invocation of graphics

Source: Internet
Author: User
Tags webservice annotation wsdl

WebService Introduction:
WEB Service technology enables different applications running on different machines to exchange data or integrate with each other without the use of additional, specialized third-party software or hardware. Applications that are implemented according to the Web Service specification can exchange data with each other, regardless of the language, platform, or internal protocol they are using. Web service is a self-describing, self-contained, available network module that can perform specific business functions. Web service is also easy to deploy because they are based on some common industry standards and some of the technologies that are available, such as subset XML, HTTP, in standard generic markup language. Web service reduces the cost of application interfaces. WEB Service provides a common mechanism for integration of business processes across the enterprise and even across multiple organizations

First, WebService generation

1. Create a new Web project in Eclipse or Myclipse and create a new class class for publishing;

2. Write a method for the client to call, that is, compile the method code;
Example:

Import Javax.jws.WebMethod;
Import Javax.jws.WebService;
Import Javax.xml.ws.Endpoint;

/**

  • Title:servicehello
  • Description: Javax.jws release WebService interface based on jdk1.6
    @WebService-it is an annotation that is specified on the class to publish this class as a WS.
    Endpoint– This class is the endpoint service class, its method publish is used to add a @webservice annotation that has already been added
    The object is bound to an address on the port.
  • version:1.0.0
  • @author panchengming
    */
    @WebService
    public class Jwsservicehello {

    /** for Client call methods The method is non-static and will be published

      • @param name passed in parameter
      • @return String Returns the result
      • */
        public string GetValue (string name) {
        Return "Welcome to you!" "+name;
        }

    /**

      • Method is added @webmentod (exclude=true), this method is not published;
      • @param name
      • @return
        */
        @WebMethod (Exclude=true)
        public string Gethello (string name) {
        Return "Hello!" "+name;
        }

    /** static methods are not published

      • @param name
      • @return
        */
        public static string getString (string name) {
        Return "Goodbye!" "+name;
        }

        Publish a WebService through endpoint (endpoint service)
        public static void Main (string[] args) {
        / parameter: 1, local Service address;
        2, the provision of service classes;
        /
        Endpoint.publish ("Http://192.168.1.105:8080/Service/ServiceHello", New Jwsservicehello ());
        System.out.println ("released successfully!");
        After the publication is successful, enter HTTP://192.168.1.105:8080/SERVICE/SERVICEHELLO?WSDL in the browser
        }
        }
        3. Run the compilation (note: Failed check JDK version)

        4. Add the address of the set to the browser access, such as XML format code, the publication succeeds, otherwise, no;

        Second, WebService call method

        Generate Code Mode one

1. Create a new class class to invoke WebService. Right-click SRC, locate the Web Service Client, enter the WSDL address, select the path to download the code; (URL:HTTP://192.168.1.105:8080/SERVICE/SERVICEHELLO?WSDL)



2. Download the files from the address (note and publish the JDK);
3. Write the method called in the Java class in the WebService downloaded from the method call;
Example:

Import Com.pcm.ws.jws.JwsServiceHello;
Import Com.pcm.ws.jws.JwsServiceHelloService;

/**

      • Title:jwsclienthello
      • Description:webservice Client Calls
      • version:1.0.0
      • @author panchengming
        */
        public class Jwsclienthello {

      public static void Main (string[] args) {
      Call WebService
      Jwsservicehello hello=new Jwsservicehelloservice (). Getjwsservicehelloport ();
      String name=hello.getvalue ("panchengming");
      SYSTEM.OUT.PRINTLN (name);
      }
      }

      Generate Code Mode two
      1. Create a Web Service Project named Theclient.
      2. Under DOS command, enter wsimport-s "src directory"-P "generated class with package name"-keep "WSDL publishing Address"
      Example: Wsimport-s G:\workspace\webService\webService_Project\src-p com.pcm.ws.jws-keep http://192.168.1.105:8080/ service/servicehello?wsdl
      3. Success returns parsing WSDL ...
      Generating code ...
      Compiling code ...
      4. Refresh the project to generate the class;
      5, Write Call method (IBID.);

Three, Summary:

How to publish a Web service:
A, add @webservice annotations on the class
(Note: This note is provided by jdk1.6 and is located in the Javax.jws.WebService package)
b, publish a WebService via endpoint (endpoint service)
(Note: Endpoint is a class dedicated to publishing services provided by the JDK, and the publish method of the class receives two parameters, one is the local service address, and the other is the class that provides the service.) Located in the Javax.xml.ws.Endpoint package)
C, Note:
Add annotation @webservice to the class, and all non-static methods in the class will be published;
Static methods and final methods cannot be published;
Method is added @webmentod (exclude=true), this method is not published;

Java WebService interface Generation and invocation of graphics

Related Article

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.