• day63-webservice 06.在web项目中发布以类的形式发布webservice


    真正用的时候都是需要部署在WEB服务器里面。

    不能写主函数来发布了,需要借助于我们WEB.

    4.配置web.xml, 

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
        <servlet>
            <servlet-name>cxf</servlet-name>
            <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>cxf</servlet-name>
            <url-pattern>/services/*</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>60</session-timeout>
        </session-config>
    </web-app>

    这个Servlet是初始化WebService用的.

    创建cxf的核心配置文件cxf-servlet.xml

    <?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" />
        
        <jaxws:endpoint id="hello" address="/hello" implementor="com.rl.cxf.web.server.HelloService">
            <jaxws:outInterceptors>
                <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
            </jaxws:outInterceptors>
            <jaxws:inInterceptors>
                <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
            </jaxws:inInterceptors>
        </jaxws:endpoint>
        
    </beans>

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
      Licensed to the Apache Software Foundation (ASF) under one
      or more contributor license agreements. See the NOTICE file
      distributed with this work for additional information
      regarding copyright ownership. The ASF licenses this file
      to you under the Apache License, Version 2.0 (the
      "License"); you may not use this file except in compliance
      with the License. You may obtain a copy of the License at
     
      http://www.apache.org/licenses/LICENSE-2.0
     
      Unless required by applicable law or agreed to in writing,
      software distributed under the License is distributed on an
      "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
      KIND, either express or implied. See the License for the
      specific language governing permissions and limitations
      under the License.
    -->
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--  For Testing using the Spring commons processor, uncomment one of:--> 
        <!-- 
            <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
            <context:annotation-config/>
        -->
        <bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus" destroy-method="shutdown"/>
        
        
        <bean id="org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor" 
            class="org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor"/>
        <bean id="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor" 
            class="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor"/>    
        <bean id="org.apache.cxf.bus.spring.BusExtensionPostProcessor" 
            class="org.apache.cxf.bus.spring.BusExtensionPostProcessor"/>
    
    </beans>

    myeclipse中配置spring xml自动提示

    什么时候加载cxf-servlet.xml?启动的时候加载.

    关联源码

    /**
     * Licensed to the Apache Software Foundation (ASF) under one
     * or more contributor license agreements. See the NOTICE file
     * distributed with this work for additional information
     * regarding copyright ownership. The ASF licenses this file
     * to you under the Apache License, Version 2.0 (the
     * "License"); you may not use this file except in compliance
     * with the License. You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing,
     * software distributed under the License is distributed on an
     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     * KIND, either express or implied. See the License for the
     * specific language governing permissions and limitations
     * under the License.
     */
    package org.apache.cxf.transport.servlet;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.servlet.ServletConfig;
    
    import org.apache.cxf.Bus;
    import org.apache.cxf.BusFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.core.io.Resource;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    import org.springframework.web.context.support.XmlWebApplicationContext;
    
    public class CXFServlet extends CXFNonSpringServlet {
        private boolean busCreated;
        private XmlWebApplicationContext createdContext; 
        
        public CXFServlet() {
        }
    
        @Override
        protected void loadBus(ServletConfig sc) {
            ApplicationContext wac = WebApplicationContextUtils.
                getWebApplicationContext(sc.getServletContext());
            String configLocation = sc.getInitParameter("config-location");
            if (configLocation == null) {
                try {
                    InputStream is = sc.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");
                    if (is != null && is.available() > 0) {
                        is.close();
                        configLocation = "/WEB-INF/cxf-servlet.xml";
                    }
                } catch (Exception ex) {
                    //ignore
                }
            }
            if (configLocation != null) {
                wac = createSpringContext(wac, sc, configLocation);
            }
            if (wac != null) {
                setBus(wac.getBean("cxf", Bus.class));
            } else {
                busCreated = true;
                setBus(BusFactory.newInstance().createBus());
            }
        }
    
        /**
         * Try to create a spring application context from the config location.
         * Will first try to resolve the location using the servlet context.
         * If that does not work then the location is given as is to spring
         * 
         * @param ctx
         * @param sc
         * @param configLocation
         * @return
         */
        private ApplicationContext createSpringContext(ApplicationContext ctx,
                                                       ServletConfig sc,
                                                       String location) {
            XmlWebApplicationContext ctx2 = new XmlWebApplicationContext();
            createdContext = ctx2;
            ctx2.setServletConfig(sc);
    
            Resource r = ctx2.getResource(location);
            try {
                InputStream in = r.getInputStream();
                in.close();
            } catch (IOException e) {
                //ignore
                r = ctx2.getResource("classpath:" + location);
                try {
                    r.getInputStream().close();
                } catch (IOException e2) {
                    //ignore
                    r = null;
                }
            }
            try {
                if (r != null) {
                    location = r.getURL().toExternalForm();
                }
            } catch (IOException e) {
                //ignore
            }        
            if (ctx != null) {
                ctx2.setParent(ctx);
                String names[] = ctx.getBeanNamesForType(Bus.class);
                if (names == null || names.length == 0) {
                    ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml",
                                                          location});                
                } else {
                    ctx2.setConfigLocations(new String[] {location});                                
                }
            } else {
                ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml",
                                                      location});
                createdContext = ctx2;
            }
            ctx2.refresh();
            return ctx2;
        }
        public void destroyBus() {
            if (busCreated) {
                //if we created the Bus, we need to destroy it.  Otherwise, spring will handleit.
                getBus().shutdown(true);
            }
            if (createdContext != null) {
                createdContext.close();
            }
        }
    
    }

    loadBus(ServletConfig sc)方法什么时候被调用?Servlet什么时候被初始化?初始化应该调用一个方法(init()方法).

    当你把这个里面的东西继承过来之后就不分什么外层内层的了,就是说都在一个类里面这些东西啊。

    Spring的接口ApplicationContext是容器的接口.WebApplicationContextUtils是工具类,当然还有一个东西叫WebApplicationContext,它是ApplicationContext的一个子类,是Web容器对于Ioc容器的支持.

    web.xml可以有一些参数的一些东西,可以有一些参数.

    web.xml如果是手动指定路径那么configLocation不等于null.

            if (configLocation != null) {
                wac = createSpringContext(wac, sc, configLocation);
            }

    如果configLocation不等于null,那么它就去加载初始化init File里面的路径了.

            if (configLocation == null) {
                try {
                    InputStream is = sc.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");
                    if (is != null && is.available() > 0) {
                        is.close();
                        configLocation = "/WEB-INF/cxf-servlet.xml";
                    }
                } catch (Exception ex) {
                    //ignore
                }
            }

    如果configLocation等于null,就去读配置文件WEB-INF/cxf-servlet.xml.配置文件默认的位置是WEB-INF/cxf-servlet.xml.

    咱们初始化的时候,在Servlet里面,那就说明在web.xml,当你交代了在这个base创建的时候,init的时候,这个配置文件cxf-servlet.xml就被读取了.读取了之后还要把服务给你发布出去.

         <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>

    做好了之后可以把这个服务部署在WEB服务里面.

    WSDL文档是从下往上看

    它会先去读WSDL文档,然后再去发送请求

    这是发送的消息体:

    <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHello xmlns:ns2="http://server.web.rl.com/"><arg0>李四</arg0></ns2:sayHello></S:Body></S:Envelope>

    这是返回的消息体:

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://server.web.rl.com/"><return>李四 hello</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>

    在web.xml里面配置CXFServlet.CXFServlet是用作初始化用的,cxf-servlet.xml是核心的配置文件.核心的配置文件是用于为我们的这个服务类来做的这个配置,指定地址啊,指定这个服务类啊。



    package
    com.rl.web.server; import javax.jws.WebService; @WebService //所有的webservice服务类都要加@webservice,接口的形式加在接口上 public class HelloService { public String sayHello(String name){ return name + " hello"; } }
    <?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" />
        <!-- 
           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>
    </beans>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
       <servlet>
           <servlet-name>cxf</servlet-name>
           <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    
       </servlet>
       <servlet-mapping>
           <servlet-name>cxf</servlet-name>
           <url-pattern>/service/*</url-pattern>  <!-- 拦截这种请求 -->
       </servlet-mapping>
    </web-app>
    package com.rl.cxf.web.client;
    
    import com.rl.web.server.HelloService;
    import com.rl.web.server.HelloServiceService;
    
    public class WebClient {
        public static void main(String[] args) {
            HelloServiceService hss = new HelloServiceService();
            HelloService hs = hss.getHelloServicePort();
            String result = hs.sayHello("李四");
            System.out.println(result);
        }
    }
  • 相关阅读:
    失落时就励志一下
    域名续费一年
    BIM新时代背景下的建筑业技术变革
    培养自信的自己
    吧台人很多
    BlogEngine学习一:操作符重载
    CSS Sprite打造个性化导航
    WinForm中的DataGridView子窗体刷新父窗体备忘
    WinForms下的SliderButtons设计
    GridView中实现DropDownList联动
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/7670644.html
Copyright © 2020-2023  润新知