• day63-webservice 10.jquery的调用webservice小练习


    客户端是采用jquery方式来做调用.但是这种调用,因为jquery这种调用你就得有消息体.我们得先拿到这种消息体.PersonService这个服务类有两个方法.

    http://localhost:8080/cxf-web-server1/service/person?wsdl

    http://localhost:8080/cxf-web-server1/service/person?wsdl是SOAP1.1才有用,SOAP1.2不行.

     请求的消息体:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://person.web.rl.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <soapenv:Body>
        <q0:addPerson>
          <arg0>
            <address>北京</address>
            <gender></gender>
            <name>任亮</name>
          </arg0>
        </q0:addPerson>
      </soapenv:Body>
    </soapenv:Envelope>

    响应的消息体,查询的时候的消息体.

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getPersonAllResponse xmlns:ns2="http://person.web.rl.com/"><return><address>北京</address><gender></gender><name>任亮</name></return></ns2:getPersonAllResponse></soap:Body></soap:Envelope>

    写客户端/cxf-web-4jquery-client/WebRoot/jquery_person_ws.html点添加的时候要调一次,点查询的时候又调一次.

    服务的类的地址,服务的类的地址具体调哪一个方法.

    上一节课的疑问,Ajax里面指定好了这是服务的类.但是没有指定具体我们要请求哪一个方法?直接从消息体就能分析出来我们是请求哪一个方法.

    分析WSDL文档,从服务访问点集合PersonServiceService找到portType——PersonService之后.portType——PersonService就有了两个operation——addPerson和getPersonAll.每一个方法operation(报文)里面都有一个输入消息和输出消息.那么这个消息具体的约束是由谁约束?是由上面的这些Schema跟你做好了这种约束.那么它如何识别你调用哪个方法是根据消息来识别的.如果你传的是getPersonAll()这个消息,WebService它就知道能解析这个消息.addPerson正好对应着的是谁呢?它能解析addPerson这个东西.addPerson它代表了你调用的是哪个方法.

    <arg0>
            <address>北京</address>
            <gender></gender>
            <name>任亮</name>
          </arg0>

    <arg0></arg0>是方法main的参数.它是根据这个消息体addPerson来识别的.你在Ajax传这么一个消息它就知道你是调用addPerson这么一个消息.点添加添加成功之后

    请求消息从这拿一下.getPersonAll的请求消息是

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://person.web.rl.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <soapenv:Body>
        <q0:getPersonAll/>
      </soapenv:Body>
    </soapenv:Envelope>

    会一个其他都会了.append和appendTo啥区别呢?

    新版chrome浏览器需要添加Chrome插件/扩展程序Charset.

     


    <!DOCTYPE html>
    <html>
      <head>
        <meta name="content-type" content="text/html; charset=UTF-8">
        <title>jquery_person_ws.html</title>
        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
      <script type="text/javascript" src="js/jquery.js"></script>
      <script type="text/javascript">
        $(function(){
        $("#add").click(function (){
           //获得Person基本数据
           var name = $("#pname").val();//name还是采用id选择器.
           var gender = $("#gender").val();
           var address = $("#address").val();
           //组装消息体
           var data =  
            '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://person.web.rl.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
                         + '<soapenv:Body>'
                         + '<q0:addPerson>'
                         + ' <arg0> '
                         + ' <address>'+address+'</address>'
                         + ' <gender>'+gender+'</gender>'
                         + ' <name>'+name+'</name>'
                         + '     </arg0>'
                         + '   </q0:addPerson>'
                         + '</soapenv:Body>'
                         + '</soapenv:Envelope>';//消息体
           $.ajax({
             //JSON对象
             url:'http://localhost:8080/cxf-web-server1/service/person',//服务的类的地址,服务的类的地址具体调哪一个方法.Ajax里面指定好了这是服务的类.但是没有具体指定我们要请求哪一个方法.具体要请求哪一个方法?
             type:'post',//请求方式
             dataType:'xml',//返回值的数据类型
             contentType:'text/xml;charset=UTF-8',//指定发送的数据类型
             data:data,//发送的消息体
             success:function(responseText){//返回的是XML文档类型.
                    //解析消息体
                    //var returnObj = $(responseText).find("return");//通过$把responseText换成jquery文档类型,jquery的文档类型.jquery的文档类型你用jquery来查找就更简单了.
                    //通过jquery的方法来找return元素
                    //alert(returnObj.text());
                    alert("添加成功");//返回值就是void
                 },
                 error:function(){
                     alert('system error');//加分号规范一些
                 }
                 
             
             
             
             
           });
           
        });//点击事件
        
        $("#select").click(function (){
           
           //组装消息体
           var data =  
            '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://person.web.rl.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
            +'<soapenv:Body>'
            +'<q0:getPersonAll/>'
            +'</soapenv:Body>' 
            +'</soapenv:Envelope>';//消息体
           $.ajax({
             //JSON对象
             url:'http://localhost:8080/cxf-web-server1/service/person',//服务的类的地址,服务的类的地址具体调哪一个方法.Ajax里面指定好了这是服务的类.但是没有具体指定我们要请求哪一个方法.具体要请求哪一个方法?
             //不用变,依然是请求的是这个服务类.
             type:'post',//请求方式
             dataType:'xml',//返回值的数据类型
             contentType:'text/xml;charset=UTF-8',//指定发送的数据类型
             data:data,//发送的消息体
             success:function(responseText){//返回的是XML文档类型.
                    //解析消息体
                    //把响应的消息解析一下,解析之后给它添加到这个div里面来.
                    $("#tdiv").empty();
                    var jqueryObj = $(responseText)//把responseText先转换成jquery对象,使用$把responseText转换成jquery object
                    var returns = jqueryObj.find("return");//每一个return它是一个对象
                    var result = '';
                    returns.each(function() {
                                //循环它可以拿到每一个return,放到div里面去       
                                //先把address等基本数据给它解析出来   
                                var pname = $(this).find('name').text();
                                var address = $(this).find('address').text();
                                var gender = $(this).find('gender').text();    
                                result = result + pname + "         " +gender  + "     " + address + "<br>"; 
           
                                    }); 
                        
                    //returns不是一个对象,是一个数组
                    $("#tdiv").append(result);//根据id选择器拿到div
                 },
                 error:function(){
                     alert('system error');//加分号规范一些
                 }
                 
             
             
             
             
           });
           
        });//点击事件
        
        
        });
      </script>
      </head>
      
      <body>
                 姓名:<input type="text" id="pname"><br>
                 性别:<input type="text" id="gender"><br>
                 地址 :<input type="text" id="address"><br>
          <input type="button" id="add" value="添加">
          <input type="button" id="select" value="查询">
          <div id="tdiv" style="border: 1px solid ; 400px;height: 400px;">
             
          </div>          
      </body>
    </html>
    <?xml version="1.0" encoding="UTF-8"?> 
       <web-app  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           id="WebApp_ID" version="2.5">
       <!-- 使用Spring的监听器 -->
       <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!-- 初始化Spring的容器,cxf.xml本身就是一个Spring的容器.可以把cxf.xml作为Spring的容器进行加载. --> 
          <!-- 能加载Spring文件的类,这个类叫什么? -->
       </listener>
       <context-param>
          <param-name>contextConfigLocation</param-name><!-- param-name不能再指定config-location,而是要指定ContextLoaderListener里面读取Spring文件的那个Key -->
          <param-value>classpath:cxf.xml</param-value>
       </context-param>
       <servlet>
           <servlet-name>cxf</servlet-name>
           <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
           <!--  
           <init-param>
              <param-name>config-location</param-name>
              <param-value>classpath:cxf.xml</param-value>
           </init-param>
           <load-on-startup>1</load-on-startup>
           -->
       </servlet>
       <servlet-mapping>
           <servlet-name>cxf</servlet-name>
           <url-pattern>/service/*</url-pattern>  <!-- 拦截这种请求 -->
       </servlet-mapping>
    </web-app>
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
        xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans.xsd
                http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
                http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
        <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
        <import resource="classpath:META-INF/cxf/cxf.xml" />
        <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
        <!-- 
         =========================配置类的形式webservice服务==================================
           address:tomcat的host http://ip:port/projectName/service/后面的一端路径
           implementor:指定具体的服务的类
         -->
         <!--  
         <jaxws:endpoint id="hello" address="/hello" implementor="com.rl.web.server.HelloService"> 
            -->
            <!-- 输入拦截器,打印输入的消息 -->
            <!--  
            <jaxws:inInterceptors>
              <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
            </jaxws:inInterceptors>
            <jaxws:outInterceptors>
               <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
            </jaxws:outInterceptors>
         </jaxws:endpoint>
         -->
              <!-- 
              =======================配置带有接口的webservice服务=========================================
               address:tomcat的host http://ip:port/projectName/service/后面的一端路径
               http://ip:port/projectName/service/bye
              implementor:指定具体的服务的类
              serviceClass:服务接口的类
              -->
              <!--  
              <jaxws:server address="/bye" serviceClass="com.rl.web.server.inter.ByeInter">     
              -->
              <!-- 
                          这里不是指定实例,而是指定实现类的类
                          服务接口的实现类
              -->
              <!--  
              <jaxws:serviceBean>
                  <bean class="com.rl.web.server.inter.ByeInterImpl"></bean>
              </jaxws:serviceBean>
              -->
              <!-- 输入拦截器,打印输入的消息 --> 
            <!--
            <jaxws:inInterceptors>
              <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
            </jaxws:inInterceptors>
            <jaxws:outInterceptors>
               <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
            </jaxws:outInterceptors>
         </jaxws:server>
               --> 
                
          <jaxws:server address="/person" serviceClass="com.rl.web.person.PersonService">    
     
              <!-- 
                          这里不是指定实例,而是指定实现类的类
                          服务接口的实现类
              -->
                
              <jaxws:serviceBean>
                  <bean class="com.rl.web.person.PersonServiceImpl"></bean>
              </jaxws:serviceBean>
    
              <!-- 输入拦截器,打印输入的消息 --> 
     
            <jaxws:inInterceptors>
              <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
            </jaxws:inInterceptors>
            <jaxws:outInterceptors>
               <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
            </jaxws:outInterceptors>
            
         </jaxws:server> 
         </beans>
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
        xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans.xsd
                http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
                http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
        <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
        <import resource="classpath:META-INF/cxf/cxf.xml" />
        <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
        <!-- 
           =========================配置类的形式webservice服务==================================
           address:tomcat的host http://ip:port/projectName/service/后面的一端路径
           implementor:指定具体的服务的类
         -->
         <!--  
         <jaxws:endpoint id="hello" address="/hello" implementor="com.rl.web.server.HelloService"> 
            -->
            <!-- 输入拦截器,打印输入的消息 -->
            <!--  
            <jaxws:inInterceptors>
              <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
            </jaxws:inInterceptors>
            <jaxws:outInterceptors>
               <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
            </jaxws:outInterceptors>
         </jaxws:endpoint>
         -->
         <!-- 
              =======================配置带有接口的webservice服务=========================================
               address:tomcat的host http://ip:port/projectName/service/后面的一端路径
               http://ip:port/projectName/service/bye
              implementor:指定具体的服务的类
              serviceClass:服务接口的类
          -->
     <!--  
         <jaxws:server address="/bye" serviceClass="com.rl.web.server.inter.ByeInter">
         -->
              <!-- 
                          这里不是指定实例,而是指定实现类的类
                          服务接口的实现类
              -->
              <!--  
              <jaxws:serviceBean>
                  <bean class="com.rl.web.server.inter.ByeInterImpl"></bean>
              </jaxws:serviceBean>
              -->
              <!-- 输入拦截器,打印输入的消息 -->
              <!--          
              <jaxws:inInterceptors>
              <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
            </jaxws:inInterceptors>
            <jaxws:outInterceptors>
               <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
            </jaxws:outInterceptors>
         </jaxws:server>
         -->
         <!--  
          <jaxws:server address="/person" serviceClass="com.rl.web.person.PersonService"> 
                   -->
                   <!-- 
                          这里不是指定实例,而是指定实现类的类
                          服务接口的实现类
              --> 
              <!--  
              <jaxws:serviceBean>
                  <bean class="com.rl.web.person.PersonServiceImpl"></bean>
                 
              </jaxws:serviceBean>
               -->
                        <!-- 输入拦截器,打印输入的消息 --> 
                     <!--             
            <jaxws:inInterceptors>
              <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
            </jaxws:inInterceptors>
            <jaxws:outInterceptors>
               <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
            </jaxws:outInterceptors>
         </jaxws:server> 
         -->
    </beans>
  • 相关阅读:
    正则表达式-入门初探
    pytorch 对变长序列的处理
    数位DP小结
    impala 使用记录
    2017微软第二场笔试题解
    一次分清:jvm内存结构|jmm|java对象模型
    JMM-java内存模型
    java基础总结
    有关秒杀的一点思考
    Elasticsearch 启动报了TypeError: can’t dup Fixnum 错
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/7689016.html
Copyright © 2020-2023  润新知