001 web服务概念

来源:互联网 发布:sql server 删除实例 编辑:程序博客网 时间:2024/05/22 13:19

正在学习web服务,看书笔记


web服务是什么?

1、网络程序

2、分布式应用开发的工具

特点?

1、开发架构。基于http、xml 、现有的安全等标准

2、语言透明

3、模块化:适合网络进程间的通讯和整合


网络程序1暴露一个接口iter1(包含一个或多个方法)供其他的程序调用,其他程序利用iter1的方式非常的简单,很对象化

web服务用WSDL来描述一个java服务程序的接口

采用SOAP来传送消息 。SOAP是一个在分布式环境中用来传送结构化信息的轻量级协议,用xml来描述一个消息框架




一个SEI

package ch01.ts;  // time server

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

@WebService
@SOAPBinding(style = Style.RPC) // more on this later
public interface TimeServer {
    @WebMethod String getTimeAsString();
    @WebMethod long getTimeAsElapsed();
}


一个SIB

package ch01.ts;

import java.util.Date;
import javax.jws.WebService;

@WebService(endpointInterface = "ch01.ts.TimeServer")
public class TimeServerImpl implements TimeServer {
    public String getTimeAsString() { return new Date().toString(); }
    public long getTimeAsElapsed() { return new Date().getTime(); }
}



通过EndPoit发布


package ch01.team;

import javax.xml.ws.Endpoint;

class TeamsPublisher {
    public static void main(String[] args) {
        int port = 8888;
        String url = "http://localhost:" + port + "/ts";
       
        Endpoint.publish(url, new TimeServerImpl());
    }
}


对web服务的访问

package ch01.ts;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;
class TimeClient {
    public static void main(String args[ ]) throws Exception {
        URL url = new URL("http://localhost:8888/ts?wsdl");

        // Qualified name of the service:
        //   1st arg is the service URI
        //   2nd is the service name published in the WSDL
        QName qname = new QName("http://ts.ch01/", "TimeServerImplService");

        // Create, in effect, a factory for the service.
        Service service = Service.create(url, qname);

        // Extract the endpoint interface, the service "port".
        TimeServer eif = service.getPort(TimeServer.class);

        System.out.println(eif.getTimeAsString());
        System.out.println(eif.getTimeAsElapsed());
   }
}