Java1.6提供了非常方便的发布web service的方法。
Java SE 6 SDK 内嵌了一个轻量级的 HTTP Server。这样web service不再像以往那样运行在容器里了。
下面介绍一下Java1.6 发布web service的步骤:
1. 创建一个Java工程
2. 写一个要发布的web service的类
package com.ibm.test;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService()
public class MyHelloWorld {
@WebMethod()
public String hello(String name) {
return "Hello, " + name + "\n";
}
public static void main(String[] args) {
// create and publish an endpoint
Endpoint endpoint = Endpoint.publish("http://localhost:7893/endpointhello", new MyHelloWorld());
}
}
3. 使用 apt 编译 Hello.java,产生辅助文件.运行完这条命令之后,目录下面多出了一个 jaxws 子目录
apt -d .\ Hello.java
4. 编译并运行工程
5. 访问wsdl文件,
http://localhost:7893/endpointhello
wsdl会像下面一样,证明web service创建成功
<?xml version="1.0" encoding="UTF-8" ?>
-<!--Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. --> -<!--Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. --> -<definitionsxmlns:soap=" http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns=" http://test.ibm.com/"xmlns:xsd=" http://www.w3.org/2001/XMLSchema"xmlns=" http://schemas.xmlsoap.org/wsdl/"targetNamespace=" http://test.ibm.com/"name=" MyHelloWorldService"> -<types> -<xsd:schema> <xsd:importnamespace=" http://test.ibm.com/"schemaLocation=" http://localhost:7893/endpointhello?xsd=1" /> </xsd:schema> </types> -<message name=" hello"> <partname=" parameters"element=" tns:hello" /> </message> -<message name=" helloResponse"> <partname=" parameters"element=" tns:helloResponse" /> </message> -<portType name=" MyHelloWorld"> -<operation name=" hello"> <inputmessage=" tns:hello" /> <outputmessage=" tns:helloResponse" /> </operation> </portType> -<binding name=" MyHelloWorldPortBinding"type=" tns:MyHelloWorld"> <soap:bindingtransport=" http://schemas.xmlsoap.org/soap/http"style=" document" /> -<operation name=" hello"> <soap:operationsoapAction=" " /> -<input> <soap:bodyuse=" literal" /> </input> -<output> <soap:bodyuse=" literal" /> </output> </operation> </binding> -<service name=" MyHelloWorldService"> -<port name=" MyHelloWorldPort"binding=" tns:MyHelloWorldPortBinding"> <soap:addresslocation=" http://localhost:7893/endpointhello" /> </port> </service> </definitions>