Publish WebService service in Java project--simple instance

Source: Internet
Author: User
Tags webservice annotation wsdl

1, publish a WebService service in the Java project:

How to publish?

-- JDK 1.6 in Jax-ws The specification defines how to publish a WebService services;

(1) with JDK 1.6.0_21 release of future releases;

(2) with the Web Service-related classes, all located in Javax . jws.* in the package

@WebService --This annotation is used on the class to specify that the class be published as a WebService ;

EndPoint --This class is the endpoint service class, where Publish () method is used to add a @WebService The annotation object is bound to an address on the port for publishing.

2, example: (1) release WebService service


Hellowebservice.java:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package cn.tgb.ws;importjavax.jws.webmethod;importjavax.jws.webservice;importjavax.xml.ws.endpoint;/** *  @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, and its method publish is used to bind an @webservice annotation object that has been added to an address on the port.  * @author xuemin * */@WebServicepublic Classhellowebservice {publicstring helloword (String name) {return "Hello:" + Name;} /** * After adding exclude=true, the HelloWord2 () method will not be published * @param name * @return */@WebMethod (exclude=true) publicstring HelloWord2 ( String name) {return "Hello:" +name;} publicstatic void Main (string[] args) {/** * parameter 1: Service's publish Address * Parameter 2: Service's implementation */endpoint.publish ("http://192.168.24.138:456/ Helloword ", New Hellowebservice ()); }}</span>

Note:

@WebService - It is an annotation that is used on a class to specify that the class be published as a ws.

endpoint– This class is the endpoint service class, and its methods Publish used to add an already added @WebService The annotation object is bound to a port on an address.

Run the above program for publishing.

(2) view wsdl

Address:http://192.168.24.138:456/helloWord?wsdl

as long as the client browser can see this WSDL document stating that the service was published successfully


The above services are published successfully;

Summarize:

How to publish a Web Service:

A, adding on a class @WebService Annotations

(Note: This annotation is JDK 1.6 provided, located in Javax.jws.WebService in the package)

B, through EndPoint ( endpoint Service ) Publish a WebService

(Note: EndPoint is a JDK provides a class that is dedicated to publishing the service, the class's Publish The method receives two parameters, one is the local service address, and the second is the class that provides the service. Located in the Javax.xml.ws.Endpoint package)

C , note:

adding annotations on a class @WebService , all non-static methods in the class will be published;

static Methods and Final method cannot be published;

method, add @WebMentod (exclude=true) later, this method is not released;

(3) Client access to published services

according to WSDL document to write code on the client, access the published service;

However, WSDL What if I can't read the document? How is the code written?

--You can't read it, JDK can understand, Wsimport is a JDK the self-brought, can be based on WSDL The document generates a tool for the client calling code. Regardless of what language the server side WebService uses, soy milk generates Java code on the client . So it doesn't matter what language the server is written in.

ws Import.exe command parameters are well known:

- D : Build class file. Default parameters.

- S : Generate Java file

- P : Custom Package Structure

Parse address generate source code to E Disk:


after you execute the above command, you can E the following packages and classes are generated under the disk


Note: You can also customize the package structure:

Commands for customizing the package structure:



The package that will be generated and . Java files copied to client:


to create a class to access the service: myclient . Java:


Myclient.java:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package cn.tgb.ws; Public Classmyclient {   publicstatic void Main (string[] args) {     Hellowebserviceservicehwss = new Hellowebserviceservice ();      Hellowebservicehws = Hwss.gethellowebserviceport ();      Stringresult=hws.helloword ("Hanxuemin");     SYSTEM.OUT.PRINTLN (result);  } }</span>

(4) operation result:



Summary:

Through this blog, you should grasp: how to publish a Java Web service, how to access the published Web service.

Publish WebService service in Java project--simple instance

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.