webservice:
    就是应用程序之间跨语言的调用
    wwww.webxml.com.cn
    1.xml
    2.    wsdl: webservice description language web服务描述语言
        通过xml格式说明调用的地址方法如何调用,可以看错webservice的说明书
    
    3.soap simple object access protoacl (简单对象访问协议) 
        限定了xml的格式
        soap 在http(因为有请求体,所以必须是post请求)的基础上传输xml数据
            请求和响应的xml 的格式如:    <Envelop>
                                <body>
                                //....
                                </body>
                            </Envelop>
                operation name:服务提供的方法
                
        
    静态方法不能发布为外部服务
    
    运用jkd自带的代码生成访问服务器的客户端代码    E:/wsimort -s . http://test.cm/?wsdl
    
    我们可以把webservice看做是web服务器上的一个应用,web服务器是webservice的一个容器
    
    函数的参数在 http://test.cm/?xsd=1
    
    JAX-WS是指 java api for xml -WebService
    
    //测试 WebService服务的 explorer
    Web Service Explorer 可以显示返回的xml格式
    
    targetNamespace 默认为倒置的包名
    
客户端调用WebService的方式:
    1.通过wximport生成代码
    2.通过客户端编程方式
    3.通过ajax调用方式
    4.通过 URL Connection 方式调用

请求过程分析:
        1.使用get方式获取wsdl文件,称为握手
        2.使用post发出请求
        3.服务器响应成功过

几种监听工具:
    http watch
    Web Service explorer
    eclipse 自带工具   TCP/IP Monitor

    服务端代码:

package com.webservcie;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

/**
 * WebService
 * 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口
 */
@WebService(serviceName="MyService",targetNamespace="http://www.baidu.com")
public class HelloService {

    @WebMethod(operationName="AliassayHello")
    @WebResult(name="myReturn")
    public String sayHello(@WebParam(name="name") String name){
        return  "hello: " + name;
    }

    public String sayGoodbye(String name){

        return  "goodbye: " + name;
    }

    @WebMethod(exclude=true)//当前方法不被发布出去
    public String sayHello2(String name){
        return "hello " + name;
    }

    public static void main(String[] args) {
        /**
         * 参数1:服务的发布地址
         * 参数2:服务的实现者
         *  Endpoint  会重新启动一个线程
         */
        Endpoint.publish("http://test.cm/", new HelloService());
        System.out.println("Server ready...");
    }

}

1.客户端调用(wximport自动生成代码 【推荐】)

package com.wsclient;

public class App {

    /**
     * 通过wsimport 解析wsdl生成客户端代码调用WebService服务
     *
     * @param args
     *
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        /**
         * <service name="MyService">
         * 获得服务名称
         */
        MyService mywebService = new MyService();

        /**
         * <port name="HelloServicePort" binding="tns:HelloServicePortBinding">
         */
        HelloService hs = mywebService.getHelloServicePort();

        /**
         * 调用方法
         */
        System.out.println(hs.sayGoodbye("sjk"));

        System.out.println(hs.aliassayHello("sjk"));

    }

}

 2.通过ajax+js+xml调用

<html>
    <head>
        <title>通过ajax调用WebService服务</title>
        <script>

            var xhr = new ActiveXObject("Microsoft.XMLHTTP");
            function sendMsg(){
                var name = document.getElementById('name').value;
                //服务的地址
                var wsUrl = 'http://192.168.1.100:6789/hello';

                //请求体
                var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
                                     ' <soapenv:Body> <q0:sayHello><arg0>'+name+'</arg0>  </q0:sayHello> </soapenv:Body> </soapenv:Envelope>';

                //打开连接
                xhr.open('POST',wsUrl,true);

                //重新设置请求头
                xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8");

                //设置回调函数
                xhr.onreadystatechange = _back;

                //发送请求
                xhr.send(soap);
            }

            function _back(){
                if(xhr.readyState == 4){
                    if(xhr.status == 200){
                            //alert('调用Webservice成功了');
                            var ret = xhr.responseXML;
                            var msg = ret.getElementsByTagName('return')[0];
                            document.getElementById('showInfo').innerHTML = msg.text;
                            //alert(msg.text);
                        }
                }
            }
        </script>
    </head>
    <body>
            <input type="button" value="发送SOAP请求" onclick="sendMsg();">
            <input type="text" id="name">
            <div id="showInfo">
            </div>
    </body>
</html>

3.URL Connection方式

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * 通过UrlConnection调用Webservice服务
 *
 */
public class App {

    public static void main(String[] args) throws Exception {
        //服务的地址
        URL wsUrl = new URL("http://192.168.1.100:6789/hello");

        HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();

        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");

        OutputStream os = conn.getOutputStream();

        //请求体
        String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://ws.itcast.cn/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                      "<soapenv:Body> <q0:sayHello><arg0>aaa</arg0>  </q0:sayHello> </soapenv:Body> </soapenv:Envelope>";

        os.write(soap.getBytes());

        InputStream is = conn.getInputStream();

        byte[] b = new byte[1024];
        int len = 0;
        String s = "";
        while((len = is.read(b)) != -1){
            String ss = new String(b,0,len,"UTF-8");
            s += ss;
        }
        System.out.println(s);

        is.close();
        os.close();
        conn.disconnect();
    }
}

4.客户单编程方式(和第一种方式一样)

//文件名:HelloService.java

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.6 in JDK 6
 * Generated source version: 2.1
 *
 */
@WebService(name = "HelloService", targetNamespace = "http://ws.itcast.cn/")
@XmlSeeAlso({

})
public interface HelloService {

    /**
     *
     * @param arg0
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "sayHello", targetNamespace = "http://ws.itcast.cn/", className = "cn.itcast.ws.client.SayHello")
    @ResponseWrapper(localName = "sayHelloResponse", targetNamespace = "http://ws.itcast.cn/", className = "cn.itcast.ws.client.SayHelloResponse")
    public String sayHello(
        @WebParam(name = "arg0", targetNamespace = "")
        String arg0);

}
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import cn.itcast.ws.wsimport.HelloService;

/**
 * 通过客户端编程的方式调用Webservice服务
 *
 */
public class App {

    public static void main(String[] args) throws Exception {
        URL wsdlUrl = new URL("http://192.168.1.100:6789/hello?wsdl");
        Service s = Service.create(wsdlUrl, new QName("http://ws.itcast.cn/","HelloServiceService"));
        HelloService hs = s.getPort(new QName("http://ws.itcast.cn/","HelloServicePort"), HelloService.class);
        String ret = hs.sayHello("zhangsan");
        System.out.println(ret);
    }
}

java 实现WebService 以及不同的调用方式的更多相关文章

  1. 【转载】Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍

    转载地址:http://blog.csdn.net/truong/article/details/46711045 关键字:Redis的Java客户端Jedis的八种调用方式(事务.管道.分布式…)介 ...

  2. Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍

    jedis是一个著名的key-value存储系统,而作为其官方推荐的java版客户端jedis也非常强大和稳定,支持事务.管道及有jedis自身实现的分布式. 在这里对jedis关于事务.管道和分布式 ...

  3. Java客户端Jedis的八种调用方式

      redis是一个著名的key-value存储系统,而作为其官方推荐的java版客户端jedis也非常强大和稳定,支持事务.管道及有jedis自身实现的分布式. 在这里对jedis关于事务.管道和分 ...

  4. java 调用wsdl的webservice接口 两种调用方式

    关于wsdl接口对于我来说是比较头疼的 基本没搞过.一脸懵 就在网上搜 看着写的都很好到我这就不好使了,非常蓝瘦.谨以此随笔纪念我这半个月踩过的坑... 背景:短短两周除了普通开发外我就接到了两个we ...

  5. Java - 在WebService中使用Client调用三方的RestAPI

    背景 近期,由于项目的要求需要在自己的webservice中调用远程的WebAPI(Restful format).自己的webservice程序是用Java编码写的,所以需要在其中实现一个Clien ...

  6. WebService与RMI(远程调用方式实现系统间通信)

    前言 本文是<分布式java应用基础与实践>读书笔记:另外参考了此博客,感觉讲的挺好的,尤其是其中如下内容: 另外,消息方式实现系统间通信本文不涉及.RMI则只采用spring RMI框架 ...

  7. webservice的两种调用方式

    如下 using ConsoleApplication1.TestWebService; using System; using System.Collections; using System.Co ...

  8. java实现WebService 以及客户端不同的调用方式

    java 实现WebService 以及不同的调用方式 webservice:    就是应用程序之间跨语言的调用    wwww.webxml.com.cn    1.xml    2.    ws ...

  9. Java调用.NET webservice方法的几种方式

    最近做项目,涉及到web-service调用,现学了一个星期,现简单的做一个小结.下面实现的是对传喜物流系统(http://vip.cxcod.com/PodApi/GetPodStr.asmx?ws ...

随机推荐

  1. C++学习笔记33:泛型编程拓展2

    调用标准模板库的find()函数查找数组元素 例子: #include <iostream> #include <algorithm> using namespace std; ...

  2. word2vec + transE 知识表示模型

    本文主要工作是将文本方法 (word2vec) 和知识库方法 (transE) 相融合作知识表示,即将外部知识库信息(三元组)加入word2vec语言模型,作为正则项指导词向量的学习,将得到的词向量用 ...

  3. css 文本气泡样式

    1.简易气泡 eg: html部分: <div class="bubble">我是气泡文本</div> css部分: //小三角.bubble:before ...

  4. Express4.x常用API(一):res

    最近在学习NodeJS,用到了express,看着官网上的API手册,打算把其中比较常用到的API根据自己理解翻译一下,方便自己学习使用. 该篇打算用来记录下express中res. 由于水平有限,希 ...

  5. Android手机录制视频 实时传输(转载)

    最近调研android视频录制.另一部手机实时观看,大致有以下几种思路. 1. android手机充当服务器,使用NanoHTTPD充当服务器,另一部手机或者pc通过输入http://手机的ip:80 ...

  6. ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  7. lnmp搭建的常见错误

    1:运行nginx时的错误 ./configure: error: the HTTP rewrite module requires the PCRE library. 解决: [root@svr11 ...

  8. 为什么有禁用Mac系统的Spotlight的需求:

    一.为什么有禁用Mac系统的Spotlight的需求: 有的网友由于使用的是相对较老的苹果电脑在运行较新的系统:也有可能你是个速度控,受不了偶尔卡卡顿顿的操作,必须将所有导致卡顿的原因全部消除:也有可 ...

  9. 查看旧版jexus命令

    查看jexus版本 curl http://localhost/info

  10. DBA-mysql-用户控制

    创建: CREATE USER 'jeffrey'@'localhost'  IDENTIFIED BY 'new_password' PASSWORD EXPIRE; 授权: Grant all o ...