zoukankan      html  css  js  c++  java
  • javaWeb服务详解(含源代码,测试通过,注释)

           javaweb服务分为两个部分,一部分是web服务端,另一部分就是你调用的客户端了。首先我说下实现web服务的简单思路:

    一、服务器端实现:
    1.添加webservice  jar包 spring支持
    2.添加一个web服务
    3.在实体类和接口以及对应的实现类中添加注解,让它们具有公开的一种能力
    4.在spring配置文件中把具有公开能力的服务进行发布

    详细步骤:
    使用spring完成服务器端的步骤:
     第一步:编写一服务接口和服务实现类(包括实体类)
     第二步:公开服务和方法
         前提:需要导入相关的jar包
         在实体类中 添加注解  @XmlRootElement(name="WeatherInfo")
         在接口和实现类中 添加注解:
                      公开方法中添加       @WebMethod
                     非公开方法中 添加    @WebMethod(exclude=true)
                          
     第三步:在spring配置文件中
        1.头部添加 命名空间
              xmlns:jaxws="http://cxf.apache.org/jaxws"

              http://cxf.apache.org/jaxws
              http://cxf.apache.org/schemas/jaxws.xsd
              http://cxf.apache.org/bindings/soap
              http://cxf.apache.org/schemas/configuration/soap.xsd              
        2.定义service的bean
          <bean id="weatherService" class="springwebService.service.impl.WeatherServiceImpl"></bean>
        
        3.定义EndPoint (端点)
         <jaxws:endpoint id="wsServiceBean" implementor="#weatherService" address="/getWeather" publish="true"></jaxws:endpoint>
        
     第四步:在web.xml中配置servlet
       <servlet>
      <servlet-name>CXFServlet</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
       <servlet-name>CXFServlet</servlet-name>
       <url-pattern>/services/*</url-pattern>
      </servlet-mapping>
     第五步:测试服务发布是否成功
     在浏览器中输入:
       http://localhost:8080/spring09webService/services/getWeather?wsdl
       或:利用myeclipse测试 点击 launch soap web service explorer(发布web程序按钮前面)
     
    //
    http://localhost:8080/spring-09server/services/getWeather?wsdl

    客户端的实现  
    二、使用spring完成客户端的配置从而调用服务的步骤:
    第一步:生成所需的文件
        1.在Dos中进入apache-cxf-2.7.6的bin目录输入 wsdl2java  http://localhost:8080/spring09webService/services/getWeather?wsdl   
    第二步:.创建web工程,把第一步生成的实体和接口放入工程中,添加spring支持,导入cxf需要的jar包
    第三步:编写spring配置文件
            <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
       <property name="serviceClass" value="springwebClient.service.IWeatherService"></property>
       <property name="address" value="http://localhost:8080/spring09webService/services/getWeather?wsdl"></property>
      </bean>
      <bean id="wsClient" class="springwebClient.service.IWeatherService" factory-bean="clientFactory" factory-method="create"></bean>
    第四步:测试
        把 wsClient当作服务,注入到Action中,直接调用方法,获取数据

       接下来看看源代码吧,走你!!!

          Dept的web服务

          Emp的web服务

  • 相关阅读:
    springmvc 之 url映射restful 及 ant
    springmvc 之 处理方法的返回值类型
    springmvc 之 数据处理
    springmvc 之 使用注解开发springmvc
    springmvc 之 配置及流程
    springmvc 之 springmvc简介,开发步骤
    mybatis 之 mybatis整合spring
    mybatis 之 mybatis缓存
    mybatis 之 mybatis的映射
    SuperMap iClient3D for WebGL教程 水面特效制作
  • 原文地址:https://www.cnblogs.com/a1111/p/7459670.html
Copyright © 2011-2022 走看看