JDK6中对Web Service的支持
原文: http://blog.csdn.net/zhongweijian/article/details/8008471
webService是一种跨语言的系统间交互标准。在java中使用webservice根据服务器端的服务根据描述生成WSDL文件,并将应用与此WSDL文件一起放入HTTP服务器中,借助服务工具根据WSDL文件生成客户端STUB代码。此代码的作用是将产生的对象请求信息封装成标准的SOAP格式数据,并发送到服务器端,服务器端根据接收到的SOAP格式数据进行转换,并最终通过反射调用响应类的响应方法。
Jdk 6中通过提供的wsimport工具,集成了WEB service的支持,通过WebService 的annotation来暴露服务的实现,并通过Endpoint.publish将服务发布到指定的地址,客户端通过wsimport来访问响应地址的wsdl文件,生成调用服务器端服务的stub类信息,客户端即可通过生成的类来调用服务器的服务了。
Usage: wsimport [options] <WSDL_URI>
where [options] include:
-b <path> specify jaxws/jaxb binding files or additional schemas
(Each <path> must have its own -b)
-B<jaxbOption> Pass this option to JAXB schema compiler
-catalog <file> specify catalog file to resolve external entity references
supports TR9401, XCatalog, and OASIS XML Catalog format.
-d <directory> specify where to place generated output files
-extension allow vendor extensions - functionality not specified
by the specification. Use of extensions may
result in applications that are not portable or
may not interoperate with other implementations
-help display help
-httpproxy:<host>:<port> specify a HTTP proxy server (port defaults to 8080)
-keep keep generated files
-p <pkg> specifies the target package
-quiet suppress wsimport output
-s <directory> specify where to place generated source files
-target <version> generate code as per the given JAXWS spec version
Defaults to 2.2, Accepted values are 2.0, 2.1 and 2.2
e.g. 2.0 will generate compliant code for JAXWS 2.0 spec
-verbose output messages about what the compiler is doing
-version print version information
-wsdllocation <location> @WebServiceClient.wsdlLocation value
-clientjar <jarfile> Creates the jar file of the generated artifacts along with the
WSDL metadata required for invoking the web service.
Extensions:
-XadditionalHeaders map headers not bound to request or response message to
Java method parameters
-Xauthfile file to carry authorization information in the format
http://username:password@example.org/stock?wsdl
-Xdebug print debug information
-Xno-addressing-databinding enable binding of W3C EndpointReferenceType to Java
-Xnocompile do not compile generated Java files
-XdisableSSLHostnameVerification disable the SSL Hostname verification while fetching
wsdls
Examples:
wsimport stock.wsdl -b stock.xml -b stock.xjb
wsimport -d generated http://example.org/stock?wsdl
具体示例如下:
1.对外暴露的接口
public interface TestWebService {
public String echo();
}
2.服务器端的实现类,并通过@WebService来指定对外提供服务的服务名称,客户端生成的类目和包名
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService(name="MyTestWS",serviceName="MyWebService",targetNamespace="http://localhost/client")
public class WebServiceImpl implements TestWebService{
@Override
public String echo() {
return "webservice return msg";
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/MyWebService", new WebServiceImpl());
}
}
3.然后运行服务器的WebServiceImpl的main函数,暴露服务并将服务注册到一个http服务地址上,客户端通过jdk的bin下面的wsimport命令来获取服务器的wsdl文件并生成客户端的stub类信息
wsimport -keep http://localhost:8080/MyWebService?wsdl
4.然后在你的路径上就会生成下面几个类
5.然后我们编写客户端的调用代码
import localhost.client.MyWebService;
public class WebServiceClient {
public static void main(String[] args) {
MyWebService myWebService = new MyWebService();
System.out.println(myWebService.getMyTestWSPort().echo());
}
}
6.执行客户端的调用代码,输出如下:
7.我们看下最终生成客户端服务调用的类内容
package localhost.client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
/**
* This class was generated by the JAXWS SI.
* JAX-WS RI 2.0_02-b08-fcs
* Generated source version: 2.0
*
*/
@WebServiceClient(name = "MyWebService", targetNamespace = "http://localhost/client", wsdlLocation = "http://localhost:8080/MyWebService?wsdl")
public class MyWebService
extends Service
{
private final static URL MYWEBSERVICE_WSDL_LOCATION;
static {
URL url = null;
try {
url = new URL("http://localhost:8080/MyWebService?wsdl");
} catch (MalformedURLException e) {
e.printStackTrace();
}
MYWEBSERVICE_WSDL_LOCATION = url;
}
public MyWebService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public MyWebService() {
super(MYWEBSERVICE_WSDL_LOCATION, new QName("http://localhost/client", "MyWebService"));
}
/**
*
* @return
* returns MyTestWS
*/
@WebEndpoint(name = "MyTestWSPort")
public MyTestWS getMyTestWSPort() {
return (MyTestWS)super.getPort(new QName("http://localhost/client", "MyTestWSPort"), MyTestWS.class);
}
}