Java6 WebService @WebService

1. 新建一个JAVA project.
2. 新建一个Hello.java,代码如下:
package study.jdk6ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(targetNamespace ="http://jdk6ws.study/client")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class Hello {
 @WebMethod
 public String sayHello(String name) {
  return "OK, " + name;
 }
}
3. 新建一个发布这个服务的启动程序(StartService.java),代码如下:
package study.jdk6ws;

import javax.xml.ws.Endpoint;

public class StartService {
 public static void main(String[] args) {
  Endpoint.publish("http://localhost:8080/HelloService", new Hello());
  System.out.println("Success");
 }
}
4. 运行这个应用程序: StartService
5. 打开浏览器,输入: http://localhost:8080/HelloService?wsdl
可以看到相应的wsdl描述:
<?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. 
  --> 
- <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://jdk6ws.study/client" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://jdk6ws.study/client" name="HelloService">
  <types /> 
- <message name="sayHello">
  <part name="arg0" type="xsd:string" /> 
  </message>
- <message name="sayHelloResponse">
  <part name="return" type="xsd:string" /> 
  </message>
- <portType name="Hello">
- <operation name="sayHello" parameterOrder="arg0">
  <input message="tns:sayHello" /> 
  <output message="tns:sayHelloResponse" /> 
  </operation>
  </portType>
- <binding name="HelloPortBinding" type="tns:Hello">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" /> 
- <operation name="sayHello">
  <soap:operation soapAction="" /> 
- <input>
  <soap:body use="literal" namespace="http://jdk6ws.study/client" /> 
  </input>
- <output>
  <soap:body use="literal" namespace="http://jdk6ws.study/client" /> 
  </output>
  </operation>
  </binding>
- <service name="HelloService">
- <port name="HelloPort" binding="tns:HelloPortBinding">
  <soap:address location="http://localhost:8080/HelloService" /> 
  </port>
  </service>
  </definitions>
6. 上述文件表明服务已发布成功。接下来写一个客户端测试一下。
7. 用命令行进入到源代码路径下(src),输入命令:
wsimport -keep http://localhost:8080/HelloService?wsdl
即可在当前目录(src)生成客户端,-keep表示保留源文件,如果没有-keep,则只生成class文件。
运行中显示如下信息:
parsing WSDL...


generating code...

8. OK,客户端代码生成完成, 会生成如下两个文件:
(Hello.java)
package study.jdk6ws.client;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.1 in JDK 6
 * Generated source version: 2.1
 * 
 */
@WebService(name = "Hello", targetNamespace = "http://jdk6ws.study/client")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface Hello {
    /**
     * 
     * @param arg0
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(partName = "return")
    public String sayHello(
        @WebParam(name = "arg0", partName = "arg0")
        String arg0);
}

(HelloService.java)
package study.jdk6ws.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;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.1 in JDK 6
 * Generated source version: 2.1
 * 
 */
@WebServiceClient(name = "HelloService", targetNamespace = "http://jdk6ws.study/client", wsdlLocation = "http://localhost:8080/HelloService?wsdl")
public class HelloService
    extends Service
{

    private final static URL HELLOSERVICE_WSDL_LOCATION;

    static {
        URL url = null;
        try {
            url = new URL("http://localhost:8080/HelloService?wsdl");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HELLOSERVICE_WSDL_LOCATION = url;
    }

    public HelloService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public HelloService() {
        super(HELLOSERVICE_WSDL_LOCATION, new QName("http://jdk6ws.study/client", "HelloService"));
    }

    /**
     * 
     * @return
     *     returns Hello
     */
    @WebEndpoint(name = "HelloPort")
    public Hello getHelloPort() {
        return (Hello)super.getPort(new QName("http://jdk6ws.study/client", "HelloPort"), Hello.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns Hello
     */
    @WebEndpoint(name = "HelloPort")
    public Hello getHelloPort(WebServiceFeature... features) {
        return (Hello)super.getPort(new QName("http://jdk6ws.study/client", "HelloPort"), Hello.class, features);
    }

}

9. 写一个测试代码Hello_ClientTest.java如下:

package study.jdk6ws.client;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Hello_ClientTest {
 public static void main(String[] args) {
  HelloService service = new HelloService();
        Hello hello = service.getHelloPort();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 
        Calendar calendar = Calendar.getInstance();
        System.out.println(hello.sayHello(format.format(calendar.getTime())));
 }
}

10. 执行结果:
OK, 2009-04-05 16:50:57.703

11. JDK6 webservice测试完成。

原文:http://dolphin-ygj.iteye.com/blog/546192

你可能感兴趣的:(Java6 WebService @WebService)