java6创建webservice,原来这么简单啊

最近在学习webService,看了很多框架,感觉很乱,后来看了个纯jdk实现的,很简单.
webService的概念我就不想说了,
首先创建一个接口:我这叫它ServiceDemo
@WebService
public interface ServiceDemo {
	public String hello(String name);

	public java.util.List<String> listUser();
}


接口的实现:
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@WebService(targetNamespace = "http://server.hw.demo/", 
		endpointInterface = "demo.java6.ServiceDemo", 
		serviceName = "ServiceDemo",
		portName="ServiceDemopott")
public class ServiceDemoImpl implements ServiceDemo {
	Log log = LogFactory.getLog(ServiceDemoImpl.class);
	List<String> list = new ArrayList<String>();

	@Resource
	private WebServiceContext wsContext;

	public ServiceDemoImpl() {
		log.info("初始化");
		System.out.println("init....");
		for (int i = 0; i < 10; i++) {
			list.add("第" + i + "个");
		}
	}

	public String hello(String name) {
		MessageContext message = wsContext.getMessageContext();
		message.get("");
		System.out.println("message:" + message.keySet());
		return "hi :" + name;
	}

	public List<String> listUser() {
		return list;
	}
}


就这么简单了,然后就是写个server发布这个webService,代码如下:
import javax.xml.ws.Endpoint;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Server {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String address = "http://localhost:9898/ServiceDemo";
		ServiceDemoImpl implementor = new ServiceDemoImpl();
		Endpoint.publish(address, implementor);
		Log log = LogFactory.getLog(Server.class);
		log.info("服务器启动...");
	}
}

服务器端这样就好了。

下面说说客户端:

客户端据我现在的了解可以有两种方式:
一个是用jdk自带的wsimport生成客户端代码,用法是这样的:
wsimport -keep http://localhost:9898/ServiceDemo?wsdl
这样就会在当前目录生成相应的客户端代码,
其中有个类叫ServiceDemo_Service,他的代码如下:

package servicedemo.java6.demo;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
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.6 in JDK 6
 * Generated source version: 2.1
 * 
 */
@WebServiceClient(name = "ServiceDemo", targetNamespace = "demo.java6.servicedemo", wsdlLocation = "http://localhost:9898/ServiceDemo?wsdl")
public class ServiceDemo_Service
    extends Service
{

    private final static URL SERVICEDEMO_WSDL_LOCATION;
    private final static Logger logger = Logger.getLogger(servicedemo.java6.demo.ServiceDemo_Service.class.getName());

    static {
        URL url = null;
        try {
            URL baseUrl;
            baseUrl = servicedemo.java6.demo.ServiceDemo_Service.class.getResource(".");
            url = new URL(baseUrl, "http://localhost:9898/ServiceDemo?wsdl");
        } catch (MalformedURLException e) {
            logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:9898/ServiceDemo?wsdl', retrying as a local file");
            logger.warning(e.getMessage());
        }
        SERVICEDEMO_WSDL_LOCATION = url;
    }

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

    public ServiceDemo_Service() {
        super(SERVICEDEMO_WSDL_LOCATION, new QName("demo.java6.servicedemo", "ServiceDemo"));
    }

    /**
     * 
     * @return
     *     returns ServiceDemo
     */
    @WebEndpoint(name = "ServiceDemoImplPort")
    public ServiceDemo getServiceDemoImplPort() {
        return super.getPort(new QName("demo.java6.servicedemo", "ServiceDemoImplPort"), ServiceDemo.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 ServiceDemo
     */
    @WebEndpoint(name = "ServiceDemoImplPort")
    public ServiceDemo getServiceDemoImplPort(WebServiceFeature... features) {
        return super.getPort(new QName("demo.java6.servicedemo", "ServiceDemoImplPort"), ServiceDemo.class, features);
    }

}


想得到ServiceDemo接口的实例可以这样
ServiceDemo_Service service = new ServiceDemo_Service();
		servicedemo.java6.demo.ServiceDemo demo = service.getServiceDemoImplPort();

这个接口的代理对象拿到了你就可以为所欲为了。

不过这样生成的代码很多,会很晕的。
还有一种是动态生成客户端代理对象的。
public final class SimpleClient {

	private static final QName SERVICE_NAME = new QName("http://server.hw.demo/", "ServiceDemo");

	private SimpleClient() {
	}

	public static void main(String args[]) throws Exception {
		Service service = Service.create(new URL("http://localhost:9898/ServiceDemo?wsdl"), SERVICE_NAME);
		ServiceDemo demo = service.getPort(ServiceDemo.class);
		System.out.println(demo.hello("me"));
		List<String> list = demo.listUser();
		for (String user : list) {
			System.out.println(user);
		}
	}

}

用Service直接为我们创建一个客户端接口的代理实现,真的是很简单。哈哈

你可能感兴趣的:(apache,jdk,xml,.net,webservice)