Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2561248
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-11-22 15:27:59

The wsgen tool is used to parse an existing web service implementation class and generates required files (JAX-WS portable artifacts) for web service deployment. This wsgen tool is available in $JDK/bin folder.

Use cases

2 common use cases for wsgen tool :

Generates JAX-WS portable artifacts (Java files) for web service deployment.
Generates WSDL and xsd files, for testing or web service client development.

Let’s see a web service implementation class, quite simple, just a method to return a string.

File :
ServerInfo.java
(在N:\workshop\webservices>这个目录创建文件package: net.cublog.ws
N:\workshop\webservices\net\cublog\ws\ServerInfo.java)
  1. package net.cublog.ws;
  2.  
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebService;
  5.  
  6. @WebService
  7. public class ServerInfo{
  8.  
  9.     @WebMethod
  10.     public String getIpAddress() {
  11.  
  12.         return "10.10.10.10";
  13.  
  14.     }
  15.  
  16. }

1. Generates JAX-WS portable artifacts (Java files)

To generate all the JAX-WS portable artifacts for above web service implementation class (ServerInfo.java), use following command :

Command : wsgen usage
  1. N:\workshop\webservices>cd N:\workshop\webservices\net\cublog\ws

  2. N:\workshop\webservices\net\cublog\ws>javac ServerInfo.java

  3. N:\workshop\webservices\net\cublog\ws>cd ..

  4. N:\workshop\webservices\net\cublog>cd ..

  5. N:\workshop\webservices\net>cd ..

  6. N:\workshop\webservices>wsgen -verbose -keep -cp . net.cublog.ws.ServerInfo

  7. warning: The apt tool and its associated API are planned to be
  8. removed in the next major JDK release. These features have been
  9. superseded by javac and the standardized annotation processing API,
  10. javax.annotation.processing and javax.lang.model. Users are
  11. recommended to migrate to the annotation processing features of
  12. javac; see the javac man page for more information.
  13. Note: ap round: 1
  14. [ProcessedMethods Class: net.cublog.ws.ServerInfo]
  15. [should process method: getIpAddress hasWebMethods: true ]
  16. [endpointReferencesInterface: false]
  17. [declaring class has WebSevice: true]
  18. [returning: true]
  19. [WrapperGen - method: getIpAddress()]
  20. [method.getDeclaringType(): net.cublog.ws.ServerInfo]
  21. [requestWrapper: net.cublog.ws.jaxws.GetIpAddress]
  22. [ProcessedMethods Class: java.lang.Object]
  23. net\cublog\ws\jaxws\GetIpAddress.java
  24. net\cublog\ws\jaxws\GetIpAddressResponse.java
  25. Note: ap round: 2

  26. N:\workshop\webservices>
In this case, it generated four files :
N:\workshop\webservices\net\cublog\ws\jaxws
GetIpAddress.java
GetIpAddress.class
GetIpAddressResponse.java
GetIpAddressResponse.class

File : GetIpAddress.java
  1. package net.cublog.ws.jaxws;

  2. import javax.xml.bind.annotation.XmlAccessType;
  3. import javax.xml.bind.annotation.XmlAccessorType;
  4. import javax.xml.bind.annotation.XmlRootElement;
  5. import javax.xml.bind.annotation.XmlType;

  6. @XmlRootElement(name = "getIpAddress", namespace = "http://ws.cublog.net/")
  7. @XmlAccessorType(XmlAccessType.FIELD)
  8. @XmlType(name = "getIpAddress", namespace = "http://ws.cublog.net/")
  9. public class GetIpAddress {


  10. }

File:GetIpAddressResponse.java
  1. package net.cublog.ws.jaxws;

  2. import javax.xml.bind.annotation.XmlAccessType;
  3. import javax.xml.bind.annotation.XmlAccessorType;
  4. import javax.xml.bind.annotation.XmlElement;
  5. import javax.xml.bind.annotation.XmlRootElement;
  6. import javax.xml.bind.annotation.XmlType;

  7. @XmlRootElement(name = "getIpAddressResponse", namespace = "http://ws.cublog.net/")
  8. @XmlAccessorType(XmlAccessType.FIELD)
  9. @XmlType(name = "getIpAddressResponse", namespace = "http://ws.cublog.net/")
  10. public class GetIpAddressResponse {

  11.     @XmlElement(name = "return", namespace = "")
  12.     private String _return;

  13.     /**
  14.      *
  15.      * @return
  16.      * returns String
  17.      */
  18.     public String getReturn() {
  19.         return this._return;
  20.     }

  21.     /**
  22.      *
  23.      * @param _return
  24.      * the value for the _return property
  25.      */
  26.     public void setReturn(String _return) {
  27.         this._return = _return;
  28.     }

  29. }



2. Genarates WSDL and xsd

To generate WSDL and xsd files for above web service implementation class (ServerInfo.java), add an extra -wsdl in the wsgen command :

Command : wsgen usage
  1. N:\workshop\webservices>wsgen -verbose -keep -cp . net.cublog.ws.ServerInfo -wsdl

  2. warning: The apt tool and its associated API are planned to be
  3. removed in the next major JDK release. These features have been
  4. superseded by javac and the standardized annotation processing API,
  5. javax.annotation.processing and javax.lang.model. Users are
  6. recommended to migrate to the annotation processing features of
  7. javac; see the javac man page for more information.
  8. Note: ap round: 1
  9. [ProcessedMethods Class: net.cublog.ws.ServerInfo]
  10. [should process method: getIpAddress hasWebMethods: true ]
  11. [endpointReferencesInterface: false]
  12. [declaring class has WebSevice: true]
  13. [returning: true]
  14. [WrapperGen - method: getIpAddress()]
  15. [method.getDeclaringType(): net.cublog.ws.ServerInfo]
  16. [requestWrapper: net.cublog.ws.jaxws.GetIpAddress]
  17. [ProcessedMethods Class: java.lang.Object]
  18. net\cublog\ws\jaxws\GetIpAddress.java
  19. net\cublog\ws\jaxws\GetIpAddressResponse.java
  20. Note: ap round: 2

  21. N:\workshop\webservices>
In this case, it generated six files :
N:\workshop\webservices\net\cublog\ws\jaxws
GetIpAddress.java
GetIpAddress.class
GetIpAddressResponse.java
GetIpAddressResponse.class
N:\workshop\webservices\ServerInfoService_schema1.xsd
N:\workshop\webservices\ServerInfoService.wsdl

File : ServerInfoService.wsdl




 
   
     
   

 

 
   
 

 
   
 

 
   
     
     
   

 

 
   
   
     
     
       
     
     
       
     

   

 

 
   
     
   

 



File:ServerInfoService_schema1.xsd

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <xs:schema version="1.0" targetNamespace="http://ws.cublog.net/" xmlns:tns="http://ws.cublog.net/" xmlns:xs="">

  3.   <xs:element name="getIpAddress" type="tns:getIpAddress"/>

  4.   <xs:element name="getIpAddressResponse" type="tns:getIpAddressResponse"/>

  5.   <xs:complexType name="getIpAddress">
  6.     <xs:sequence/>
  7.   </xs:complexType>

  8.   <xs:complexType name="getIpAddressResponse">
  9.     <xs:sequence>
  10.       <xs:element name="return" type="xs:string" minOccurs="0"/>
  11.     </xs:sequence>
  12.   </xs:complexType>
  13. </xs:schema>

Published It!

All files are ready, publish it via endpoint publisher.


  1. package net.cublog.endpoint;
  2.  
  3. import javax.xml.ws.Endpoint;
  4. import net.cublog.ws.ServerInfo;
  5.  
  6. //Endpoint publisher
  7. public class WsPublisher{
  8.  
  9.     public static void main(String[] args) {
  10.      Endpoint.publish("", new ServerInfo());
  11.  
  12.      System.out.println("Service is published!");
  13.     }
  14.  
  15. }



  1. N:\workshop\webservices\net\cublog>cd endpoint

  2. N:\workshop\webservices\net\cublog\endpoint>javac -d . WsPublisher.java
  3. WsPublisher.java:4: error: package net.cublog.ws does not exist
  4. import net.cublog.ws.ServerInfo;
  5. ^
  6. WsPublisher.java:10: error: cannot find symbol
  7. Endpoint.publish("", new ServerInfo())
  8. ;
  9. ^
  10. symbol: class ServerInfo
  11. location: class WsPublisher
  12. 2 errors

  13. N:\workshop\webservices\net\cublog\endpoint>cd ..

  14. N:\workshop\webservices\net\cublog>cd ws

  15. N:\workshop\webservices\net\cublog\ws>javac -d . WsPublisher.java

  16. N:\workshop\webservices\net\cublog\ws>java net.cublog.endpoint.WsPublisher
  17. Service is published!




----------------------------------------------------------------------------------------------------------
附加常识:

>Hi,

>
>Can anyone tell me how to compile java files in a package using javac? Thanks
>
>Sean

you need to type this command line
javac -d . Test.java
that will gernreate the package for you
example for that

package com.sun.java.test
public class Test {
public static void manin(String args[] )
{
System.out.println( "hello world" );
}

when you complie this class by using javac -d . Test.java , that will gerrante
this folder com/sun/java/test/Test.class

to run this Class , you need to type this command line
java com.sun.java.test.Test





阅读(2351) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~