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)package net . cublog. ws;
import javax . jws. WebMethod;
import javax . jws. WebService;
@WebService
public class ServerInfo{
@WebMethod
public String getIpAddress( ) {
return "10.10.10.10" ;
}
} 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 usageN:\workshop\webservices>cd N:\workshop\webservices\net\cublog\ws
N:\workshop\webservices\net\cublog\ws>javac ServerInfo.java
N:\workshop\webservices\net\cublog\ws>cd ..
N:\workshop\webservices\net\cublog>cd ..
N:\workshop\webservices\net>cd ..
N:\workshop\webservices>wsgen -verbose -keep -cp . net.cublog.ws.ServerInfo
warning: The apt tool and its associated API are planned to be
removed in the next major JDK release. These features have been
superseded by javac and the standardized annotation processing API,
javax.annotation.processing and javax.lang.model. Users are
recommended to migrate to the annotation processing features of
javac; see the javac man page for more information.
Note: ap round: 1
[ProcessedMethods Class: net.cublog.ws.ServerInfo]
[should process method: getIpAddress hasWebMethods: true ]
[endpointReferencesInterface: false]
[declaring class has WebSevice: true]
[returning: true]
[WrapperGen - method: getIpAddress()]
[method.getDeclaringType(): net.cublog.ws.ServerInfo]
[requestWrapper: net.cublog.ws.jaxws.GetIpAddress]
[ProcessedMethods Class: java.lang.Object]
net\cublog\ws\jaxws\GetIpAddress.java
net\cublog\ws\jaxws\GetIpAddressResponse.java
Note: ap round: 2
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 package net . cublog. ws. jaxws;
import javax . xml . bind . annotation . XmlAccessType;
import javax . xml . bind . annotation . XmlAccessorType;
import javax . xml . bind . annotation . XmlRootElement;
import javax . xml . bind . annotation . XmlType;
@XmlRootElement( name = "getIpAddress" , namespace = "http://ws.cublog.net/" )
@XmlAccessorType( XmlAccessType. FIELD )
@XmlType( name = "getIpAddress" , namespace = "http://ws.cublog.net/" )
public class GetIpAddress {
} File:GetIpAddressResponse.javapackage net . cublog. ws. jaxws;
import javax . xml . bind . annotation . XmlAccessType;
import javax . xml . bind . annotation . XmlAccessorType;
import javax . xml . bind . annotation . XmlElement;
import javax . xml . bind . annotation . XmlRootElement;
import javax . xml . bind . annotation . XmlType;
@XmlRootElement( name = "getIpAddressResponse" , namespace = "http://ws.cublog.net/" )
@XmlAccessorType( XmlAccessType. FIELD )
@XmlType( name = "getIpAddressResponse" , namespace = "http://ws.cublog.net/" )
public class GetIpAddressResponse {
@XmlElement( name = "return" , namespace = "" )
private String _return;
/**
*
* @return
* returns String
*/
public String getReturn( ) {
return this . _return;
}
/**
*
* @param _return
* the value for the _return property
*/
public void setReturn( String _return) {
this . _return = _return;
}
} 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 usageN:\workshop\webservices>wsgen -verbose -keep -cp . net.cublog.ws.ServerInfo -wsdl
warning: The apt tool and its associated API are planned to be
removed in the next major JDK release. These features have been
superseded by javac and the standardized annotation processing API,
javax.annotation.processing and javax.lang.model. Users are
recommended to migrate to the annotation processing features of
javac; see the javac man page for more information.
Note: ap round: 1
[ProcessedMethods Class: net.cublog.ws.ServerInfo]
[should process method: getIpAddress hasWebMethods: true ]
[endpointReferencesInterface: false]
[declaring class has WebSevice: true]
[returning: true]
[WrapperGen - method: getIpAddress()]
[method.getDeclaringType(): net.cublog.ws.ServerInfo]
[requestWrapper: net.cublog.ws.jaxws.GetIpAddress]
[ProcessedMethods Class: java.lang.Object]
net\cublog\ws\jaxws\GetIpAddress.java
net\cublog\ws\jaxws\GetIpAddressResponse.java
Note: ap round: 2
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 < ? xml version = "1.0" encoding = "UTF-8" standalone= "yes" ? >
< xs:schema version = "1.0" targetNamespace= "http://ws.cublog.net/" xmlns:tns= "http://ws.cublog.net/" xmlns:xs= "" >
< xs:element name= "getIpAddress" type= "tns:getIpAddress" / >
< xs:element name= "getIpAddressResponse" type= "tns:getIpAddressResponse" / >
< xs:complexType name= "getIpAddress" >
< xs:sequence/ >
< / xs:complexType>
< xs:complexType name= "getIpAddressResponse" >
< xs:sequence>
< xs:element name= "return" type= "xs:string" minOccurs= "0" / >
< / xs:sequence>
< / xs:complexType>
< / xs:schema> Published It! All files are ready, publish it via endpoint publisher.package net . cublog. endpoint;
import javax . xml . ws. Endpoint;
import net . cublog. ws. ServerInfo;
//Endpoint publisher
public class WsPublisher{
public static void main( String [ ] args) {
Endpoint. publish ( "" , new ServerInfo( ) ) ;
System . out. println ( "Service is published!" ) ;
}
} N:\workshop\webservices\net\cublog>cd endpoint
N:\workshop\webservices\net\cublog\endpoint>javac -d . WsPublisher.java
WsPublisher.java:4: error: package net.cublog.ws does not exist
import net.cublog.ws.ServerInfo;
^
WsPublisher.java:10: error: cannot find symbol
Endpoint.publish("", new ServerInfo())
;
^
symbol: class ServerInfo
location: class WsPublisher
2 errors
N:\workshop\webservices\net\cublog\endpoint>cd ..
N:\workshop\webservices\net\cublog>cd ws
N:\workshop\webservices\net\cublog\ws>javac -d . WsPublisher.java
N:\workshop\webservices\net\cublog\ws>java net.cublog.endpoint.WsPublisher
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) |