Publish WebService service in Java project--simple instance

Source: Internet
Author: User

1. Advertise a WebService service in the Java project:

How to publish?

-- JDK 1.6 in Jax-ws The specification defines how to publish a WebService service.

(1) with JDK 1.6.0_21 The future version number is announced.

(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 advertised as a WebService ;

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

2. Examples: (1) announcement of WebService Services


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 advertise this class as a WS.    Endpoint– This class is the endpoint service class. Its method, publish, is used to bind a port that has joined the @webservice annotation object to an address.  * @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: Advertised address of service * Number of 2: Service implementation */endpoint.publish ("http://192.168.24.138:456/ Helloword ", New Hellowebservice ()); }}</span>

Note:

@WebService - It is an annotation that is specified on the class to advertise this class as a ws.

endpoint– This class is the endpoint service class. Its method, publish , is used to bind a @WebService annotation object to a port that has been joined to an address.

Implementation of the above procedures for publication.

(2) view wsdl

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

only in the client browser can you see this WSDL documentation. Explain the success of the service announcement


The above services are published successfully.

Summarize:

How to publish a Web Service:

A, join on a class @WebService Annotations

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

B, through EndPoint ( endpoint Service ) announce a WebService

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

C , note:

add annotations to the 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) , this method is not published;

(3)Client access to published services

based on WSDL document to write code in the client and access the advertised service.

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

--You can't read it, JDK I can understand. wsimport is a tool that comes with the JDK to generate client calling code from a WSDL document. Regardless of what language the server side WebService uses, soy milk generates Java code in 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. The default number of parameters.

- S : Generate Java file

- P : Define the package structure yourself

Parse address generate source code to E Disk:


after running the above command. the following packages and classes can be generated under the E -drive, for example


Note: You can also define the package structure yourself:

Commands that define the package structure yourself:



The package that will be generated and . Java copy files to client: for example,


to create an Access class: 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) Result of execution:



Summary:

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

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.