在项目中使用使用Ws服务,同时采用JDK1.6于是学习一下JDK1.6发布Web服务的应用开发、发现使用JDK1.6注解ws方便简单。
对Webservice的支持是JavaSE6的一大新特性。我们知道,想要发布一个Webservice,首先要创建相关的业务逻辑类,然后把这些业务逻辑类部署到SOAP服务器上,生成客户端访问的代码,最后部署到客户端进行测试。这是创建一个简单Webservice的主要步骤,事实上,部署和发布Webservice要复杂的多。而JavaSE6中实现了一个内置的轻量级SOAP服务器,我们可以把Webservice部署到Java平台中,并进行简单的测试。另外,JavaSE 6中加入了@Webservice等注解(Annotation),有了这些注解,创建Webservice变得十分简单。本实验将在JavaSE 6中创建和发布一个Webservice,并通过Netbeans创建Webservice客户端对已发布的Service进行测试,通过实验来理解JavaSE 6对Webservice的支持。
软件需求:jdk 1.6以上版本
下面首先看看web服务的代码,然后讲解web服务的各种信息和注意事项;
^_^
开发的代码如下:
定义web服务的接口类
package cn.com.unutrip.spring.ws;
import javax.jws.WebService;/** * * 此接口定义Web服务 * JDK1.6注解开发Ws服务 * @author longgangbai * */public interface IRemoteService { public String hello(String username);}
web服务的实现类:
package cn.com.unutrip.spring.ws;
import java.util.Date;
import javax.jws.Oneway;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.soap.SOAPBinding;/** * * * JDK1.6注解开发Ws服务 * @author longgangbai * */
@WebService(serviceName = "REMOTESERVICE", targetNamespace = "http://cn.unutrip.com/RemoteService")@SOAPBinding(style = SOAPBinding.Style.RPC)//默认的方式public class RemoteServiceImpl implements IRemoteService {
/**
*
*
*/
@WebResult(name = "Greetings")//注解返回的结果信息,可以使用默认值 @WebMethod //注解web服务的方法 public String hello(@WebParam(name = "MyName") String name) { return "Hello," + name; }
@Oneway //没有返回值 @WebMethod(action = "printSystemTime", operationName = "printSystemTime") public void printTime() { System.out.println(new Date(System.currentTimeMillis())); }
}
web服务的发布:
package cn.com.unutrip.spring.ws;
import javax.xml.ws.Endpoint;
/** * * * JDK1.6注解开发Ws服务 * * @author longgangbai * */public class WSPublish { public static void main(String[] args) { Thread wsPublisher = new Thread(new WebServicePublish()); wsPublisher.start(); }
private static class WebServicePublish implements Runnable { public void run() {
//发布web发Endpoint发布 Endpoint.publish("http://localhost:8888/HelloService", new RemoteServiceImpl()); } }}
访问路径如下:http://localhost:8888/HelloService?WSDL
访问的WSDL如下:
<?xml version="1.0" encoding="UTF-8" ?>
- < definitions xmlns =" http://schemas.xmlsoap.org/wsdl/ " xmlns:tns =" http://cn.unutrip.com/RemoteService " xmlns:xsd =" http://www.w3.org/2001/XMLSchema " xmlns:soap =" http://schemas.xmlsoap.org/wsdl/soap/ " targetNamespace =" http://cn.unutrip.com/RemoteService " name =" REMOTESERVICE "> < types /> - < message name =" hello "> < part name =" MyName " type =" xsd:string " /> </ message > - < message name =" helloResponse "> < part name =" Greetings " type =" xsd:string " /> </ message > < message name =" printSystemTime " /> - < portType name =" RemoteServiceImpl "> - < operation name =" hello " parameterOrder =" MyName "> < input message =" tns:hello " /> < output message =" tns:helloResponse " /> </ operation > - < operation name =" printSystemTime "> < input message =" tns:printSystemTime " /> </ operation > </ portType > - < binding name =" RemoteServiceImplPortBinding " type =" tns:RemoteServiceImpl "> < soap:binding style =" rpc " transport =" http://schemas.xmlsoap.org/soap/http " /> - < operation name =" hello "> < soap:operation soapAction ="" /> - < input > < soap:body use =" literal " namespace =" http://cn.unutrip.com/RemoteService " /> </ input > - < output > < soap:body use =" literal " namespace =" http://cn.unutrip.com/RemoteService " /> </ output > </ operation > - < operation name =" printSystemTime "> < soap:operation soapAction =" printSystemTime " /> - < input > < soap:body use =" literal " namespace =" http://cn.unutrip.com/RemoteService " /> </ input > </ operation > </ binding > - < service name =" REMOTESERVICE "> - < port name =" RemoteServiceImplPort " binding =" tns:RemoteServiceImplPortBinding "> < soap:address location =" http://localhost:8888/HelloService " /> </ port > </ service > </ definitions >
讲解:
注意代码中使用到的几个Annotation:
@WebService注解将 Java 类标记为实现Web Service类,或者将Java接口标记为定义Web Service接口,其中serviceName属性指定了Web Service的服务名称,当映射到WSDL时,此名称被用作 此名称被用作 wsdl:service 的名称。这一个注解对应的是javax.jws.WebService,我们可以在jdk1.6的API文档里查看到所有的属性和用法。
@WebResult指定Web Service返回的值和WSDL之间的映射。@WebMethod指定了Web Service中要暴露的方法。标记了@WebMethod的方法可以通过Web Service客户端来远程调用(RPC)。
@WebParam注解用来向Web Service传递参数的。注意,@WebMethod标注的方法必须带有返回类型,除非同时标注了@Oneway。
◎WebResult:注解放值的可以设置默认值的。如实例中。
注意使用Endpoint发布web服务信息。