JAX-WS Learning One: The simplest Java-based webservice service

Source: Internet
Author: User
Tags soap wsdl

After Java 1.6, the own Jax-ws API, which allows us to easily develop a Java-based WebService service.

Java-based WebService services

1. Create a service-side WebService service Interface

Package Com.st.services;import Javax.jws.webservice;import Javax.jws.soap.soapbinding;import Javax.jws.soap.soapbinding.style;import javax.jws.soap.SOAPBinding.Use; @WebService (servicename= "Calculator", Targetnamespace= "Com.st.services") @SOAPBinding (style=style.rpc,use=use.literal) public class CalculatorService { public int sum (int a,int b) {return a+b;}}

 

@WebService

This annotation is used to indicate that the Java class is an implementation class for a webservice or an interface that indicates that this Java interface defines a webservice.

@WebService has six parameters that can be used to configure the WebService definition:

  1、endpointInterface:指向一个定义此WebService抽象定义接口的完整类路(如果没有定义接口,直接写的实现类,则该参数不需要)

  2、name:WebService名;默认的port名为"实现类名+Port",binding名为"实现类名+PortBinding",通过指定name的值来替换 实现类名。

  3、portName:指定port名,可以完成替换默认port名,或由上面的"name"指定的port名。

  4、targetNamespace:指定targetNamespace值,默认的值为 "http://包名/",可以通过此变量指定一个自定义的targetNamespace值。(注:如果分别定义和接口和实现,则他们有各自的targetNamespace)

5.serviceName: Specify service Name

6. Wsdllocation: A file that points to a predefined WSDL instead of the automatically generated WSDL file.

! If you do not want to define, you can write an @WebService , the other properties with the default.

@WebMethod

    1. Action: Specifies the action that corresponds to this method
    2. exclude: True--Indicates that this method is included in the Web service; False indicates that this method is excluded
    3. OperationName: Specifies the name of the operation corresponding to the method.

! If you have no special requirements for your WebService method, this parameter can be left out, which will make all your methods published.

2. Publishing Service

public static void Main (string[] args) {          endpoint.publish ("Http://localhost:8088/calculator",                  New CalculatorService ());      }  

 

Here the Publish method requires two parameters:

    • Address: External exposure of service addresses for invoking services
    • Implementor: The implementation object of the service

Start the server class and you are ready to access the service. To test whether the service has started, you can enter http://localhost:8088/calculator?wsdl, and if everything is OK, you can see a WSDL definition that indicates that the service has started successfully.

3, the client program calls

The client code recommends using Wsimport to generate (provided that the JDK's environment variable is already configured on your computer)

Run directly in CMD:

Wsimport-keep-p com.st.client http://localhost:8088/calculator?wsdl

-P: defines the package name of the generated package (can not be written)

The resulting Java class is then placed in the project to be called

! Using Java to publish WebService, the default is to publish to the JDK's web container, but in the specific project, there is a separate Web container (eg:tomcat), all this is just a simple introduction.

JAX-WS Learning One: The simplest Java-based webservice service

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.