Customizing and publishing a WebService service

Source: Internet
Author: User

Customizing and publishing a WebService service
- Declare a business service to be a WebService service
To declare by @webservice annotations
- release WebService Service
Endpoint.publish () Publish (the default method for public retouching is published)
- generate a local agent via Wsimport to access your published WebService
Wsimport

1. Publish a custom WebService

Phone.java

 PackageWs.mywebservice; Public classPhone {PrivateString Phonename; PrivateString owner; Private intTotal ;  PublicString Getphonename () {returnPhonename; }     Public voidsetphonename (String phonename) { This. Phonename =Phonename; }     PublicString GetOwner () {returnowner; }     Public voidSetOwner (String owner) { This. Owner =owner; }     Public intgettotal () {returnTotal ; }     Public voidSettotal (intTotal ) {         This. Total =Total ; }}

Phoneservice. java

Writing and publishing Webservcie

@WebService  Statement
new Phoneservice ()); Publish (multiple can be published under the same port)
 PackageWs.mywebservice;ImportJavax.jws.WebService;ImportJavax.xml.ws.Endpoint;/*** Mobile phone business class, the business class through the WebService to provide external services 1. Declaration: @webservice 2. Release EndPoint*/@WebService Public classPhoneservice { Publicphone Getphoneinfo (String phonename) {phone phone=NewPhone (); if(Phonename! =NULL&& "Android". Equals (Phonename)) {Phone.setphonename ("Android"); Phone.setowner ("Google"); Phone.settotal (80); } Else if(Phonename! =NULL&& "ios". Equals (Phonename)) {Phone.setphonename ("ios"); Phone.setowner ("Apple"); Phone.settotal (15); } Else{phone.setphonename ("Windows Phone"); Phone.setowner ("Microsoft"); Phone.settotal (5); }        returnphone; }     Public Static voidMain (string[] args) {//two addresses publishedString Address1 = "Http://127.0.0.1/ws/phoneService"; String Address2= "Http://127.0.0.1:8888/ws/phoneManager"; /*** Release WebService service 1.address: Address of the service 2:implementor the implementation object of the service*/endpoint.publish (Address1,NewPhoneservice ()); Endpoint.publish (Address2,NewPhoneservice ()); System.out.println ("WSDL Address:" + Address1 + "? WSDL "); }}

Note: The WSDL will be followed when accessing

The WSDL document explains:

Note: The post address to add/HTTP, the time to add after the visit? Wsdl.

The only way to publish soap access is now the main way to access it.

2.wsimport generate a local agent for the WebService you publish

(1) Generating a local agent
C:\users\liqiang\desktop>wsimport-s./Http://127.0.0.1:8888/ws/phoneManager?WS
Dl

(2) Import Eclipse for testing:

Test class: _main.java

 PackageWs.mywebservice; Public class_main {/*** Test your published WebService * *@paramargs*/     Public Static voidMain (string[] args) {//ServicePhoneserviceservice Phoneserviceservice =NewPhoneserviceservice (); //access ModePhoneservice Phoneservice =Phoneserviceservice.getphoneserviceport (); Phone Phoneinfo= Phoneservice.getphoneinfo ("ios");        System.out.println (Phoneinfo.getowner ());        System.out.println (Phoneinfo.getphonename ());    System.out.println (Phoneinfo.gettotal ()); }}

Results:

------------------------Modify the WebService annotation configuration for the default configuration ( Custom service name and method name, parameter name )--------------------------------

Enhanced readability

Modify the WSDL file with the annotation configuration to make it more readable
Demand:
1. To change the method input parameter name, return parameter name
2. To change the service name, method name
3. Some methods of the business class do not want to be released externally
-Only public-modified methods are published by default in WebService form
Private protected, default, will not be published
-Some public methods do not want to be released externally

Implementation: Annotation configuration modifies WSDL file

(1) Publishing and generating local agents

You can modify the method name with the default package structure. You can also modify which methods are excluded from the outside, and only the public adornment method is published.

 PackageWs.it.WebService;ImportJavax.jws.WebMethod;ImportJavax.jws.WebParam;ImportJavax.jws.WebResult;ImportJavax.jws.WebService;ImportJavax.xml.ws.Endpoint;ImportWs.myWebService.Phone;/**mobile phone Business class, the business class through the WebService external services * 1. Disclaimer: @webservice * 2. Release EndPoint **/@WebService (ServiceName= "PhoneManager1",//Modify Service NameTargetnamespace= "http://dd.ws.it.cn")//Modify namespace, default package name, reverse//declares that the business class provides webservice services to the outside, by default only the method of public modification is published in WebService form Public classPhoneservice {/**@WebMethod (operationname= "Getmobileinfo"): Modify the method name * @WebResult (name= "Phone"): Modify the return parameter name * @WebParam (name= "Osname" ): Modify the input parameter name*/@WebMethod (OperationName= "Getmobileinfo")     Public@WebResult (name= "phone") Phone Getphoneinfo (@WebParam (name= "Osname") String osname) {phone phone=NewPhone (); if(Osname.endswith ("Android") {phone.setphonename ("Android");p Hone.setowner ("Google");p hone.settotal (80); }Else if(Osname.endswith ("ios") {phone.setphonename ("ios");p Hone.setowner ("Apple");p Hone.settotal (15); }Else{phone.setphonename ("Windows Phone");p Hone.setowner ("Microsoft");p Hone.settotal (5); }        returnphone; } @WebMethod (Exclude=true)//to exclude this method     Public voidSayHello (String city) {System.out.println ("Hello:" +City ); }    Private voidSayluck (String city) {System.out.println ("Friend:" +City ); }     voidSaygoodbye (String city) {System.out.println ("Bye:" +City ); }    protected voidSaysayalala (String city) {System.out.println (Goodbye "+City ); }         Public Static voidMain (string[] args) {String Address1= "Http://127.0.0.1:8088/ws/phoneSer"; /*** Release WebService Service * 1.address: Address of the service * 2:implementor Service implementation Object*/endpoint.publish (Address1,NewPhoneservice ());//Endpoint.publish (Address2, New Phoneservice ());SYSTEM.OUT.PRINTLN ("WSDL Address:" +address1+ "?) WSDL "); }}

Visit: Http://127.0.0.1:8088/ws/phoneSer?WSDL

Wsimport generate a local agent:

C:\users\liqiang\desktop>wsimport-s./Http://127.0.0.1:8088/ws/phoneSer?WSDL

Package Structure Change: (change of declaration in front of service class)

(2) Enter Eclipse for testing:

_main.java

PhoneManager1 Service Name change, above configuration caused by
The method of accessing the name also changed, which is also caused by the above configuration
 Packagecn.it.ws.dd; Public class_main {/*** Test your published WebService * *@paramargs*/     Public Static voidMain (string[] args) {//Service        PhoneManager1Phoneserviceservice =NewPhoneManager1 (); //access ModePhoneservice Phoneservice =Phoneserviceservice.getphoneserviceport (); Phone Phoneinfo=phoneservice.getmobileinfo ("ios");        System.out.println (Phoneinfo.getowner ());        System.out.println (Phoneinfo.getphonename ());    System.out.println (Phoneinfo.gettotal ()); }}

Results:

Customizing and publishing a WebService service

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.