zoukankan      html  css  js  c++  java
  • Web Service

    参考资料:

    1. http://blog.sina.com.cn/s/blog_a00f56270102w2e2.html - WebService简介+只用JDK实现

    2. http://www.cnblogs.com/linxiaoyang/p/4167016.html - JDK实现+Intellij辅助

    3. http://www.cnblogs.com/wenjingu/p/4094180.html - WebService开发汇总

    4. 推荐: http://blog.csdn.net/rongbo_j/article/details/50958111

    从名字上看,之前一直以为Web Service就是通常提的Web应用,毕竟二者都是对外提供web服务的。但实际上还是有区别的,区别为Web Service和Web Application比较合适。

    Web Service更趋向于后台服务。

    实现Web Service的框架有很多,比如JAX-WS, Axis, Axis2, CXF等,有代码优先的也有文档优先的。

    本着简单+功能强的原则,这里不涉及具体细节,比如如何编写wsdl(实际上也不会,也没必要会这个),只是单纯的实现需要的功能即可。

    ------------------------------------------------   分割线   ------------------------------------------------

    Part 1: JDK实现Web Service

    JDK提供JAX-WS(Java API for XML Web Service)来实现Web Service。相当之简单。

    直接上代码!

    1. server端

      编写Web Service

    @WebService
    public interface HelloJWS {
    
        @WebMethod
        public String execute(String msg);
    }
    @WebService(endpointInterface = "server.com.cnblogs.helloyz.HelloJWS")
    public class HelloJWSImpl implements HelloJWS {
    
        @Override
        public String execute(String msg) {
            System.out.println("Welcome! " + msg);
            return "success";
        }
    }

      对外发布

    public class PublishService {
    
        public static void main(String[] args) {
            HelloJWS jws = new HelloJWSImpl();
            String address = "http://localhost:9000/ws/HelloJWS";
            Endpoint.publish(address,jws);
            System.out.println("publish success");
        }
    }

    运行main函数,Web Service发布成功!是不是很简单?!

    浏览器访问发布地址:http://localhost:9000/ws/HelloJWS/?wsdl,可以得到如下wsdl文档。

    PS: wsdl文档语法不懂,也不想学这个。

    以上方式就属于所谓的"代码优先",根据代码生成wsdl文件。而"文档优先"方式在适应需求变化,维护等方面要优于"代码优先"。

    不过,没必要非采用"文档优先",只要功能实现就OK,况且不用专门编写wsdl(也曾想通过xsd生成wsdl,但实际上还要专门编写xsd...)。

    2. client端

    为了方便,将server端和client端弄到一个工程里了。以下几步是在创建的client包里操作。

      生成本地Web Service代码

      调出命令行窗口,执行wsimport -s . http://localhost:9000/ws/HelloJWS/?wsdl,生成本地client代码。

      编写代码调用Web Service

    public class Client {
        public static void main(String[] args) {
            HelloJWSImplService service = new HelloJWSImplService();
            HelloJWS jws = service.getHelloJWSImplPort();
            String result = jws.execute("message from MARS");
            System.out.println("client execute status: " + result);
        }
    }

    运行main,结果如下:

       

     Source Code get here

    Part 2: Web容器发布Web Service

    有不少人认为单纯用JDK实现WebService,不便于后期的管理和维护,而且可扩展性也很差。不敢苟同。

    根据上面的Demo可知,web service的编写和发布都是可以单独集中在一起的。比如,在一个@WebService里,你可以实现多个@WebMethod;在一个main方法里,你可以集中发布多个Web Service。所以不存在上面提到的问题。

    使用Web容器来发布Web Service,个人觉得主要还是考虑到Web Service作为Web服务的一种,理应由Web容器来统一管理,跟随主流。另外,相比于JDK那种通过main方法来发布服务,还是利用容器管理更稳定些。

    废话少说,直述build过程:(利用Intellij IDEA 14.1.4

    1. server端

     

    新建WebServices工程,下载21个jar包,同时生成server sample code。包结构如下:

    21个jar包在lib中。将Part 1的server端代码放到src下。设置Tomcat为WebService容器,并尝试发布。

    大致梳理下WebService发布过程:

      1. Tomcat启动,加载web.xml.

        实例化监听WSServletContextListener。此过程中加载/WEB-INF/sun-jaxws.xml

        web.xml

        <listener>
            <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
        </listener>
        <servlet>
            <description>JAX-WS endpoint</description>
            <display-name>WSServlet</display-name>
            <servlet-name>WSServlet</servlet-name>
            <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>WSServlet</servlet-name>
            <url-pattern>/services/*</url-pattern>
        </servlet-mapping>

        sun-jaxws.xml 

    <endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
    
            <endpoint
                    name='HelloWorld'
                    implementation='com.cnblogs.helloyz.HelloWorld'
                    url-pattern='/services/HelloWorld'/>
    </endpoints>

      

      接着初始化WSServlet,用于拦截/services/*请求(界定WebService和WebApplication)

      2. 根据加载的sun-jaxws.xml,创建用于发布WebService的endpoint(此处用于集中管理endpoint)。

    2. client端

    新建WebServices Client工程,下载21个jar包。和Part 1类似,生成client code。

    Source Code get here

    Part 3: Web Service与Spring集成

    又一参考:http://www.springbyexample.org/examples/simple-spring-web-services.html

    本来想按照Spring-WS的文档,实际操作遍。但是经过前前后后一天半的时间发现怎么都绕不过“文档优先”的Web Service发布方式。原以为通过使用@EndPoint和@PayloadRoot就可以,但还是需要预先定义好的wsdl。

    同时也发现GitHub上托管的Spring Web Service项目大部分代码已经两年多没更新过,网上的讨论也很少,足以说明Spring Web Service并不怎么常用。

    不过Web Service和Spring的整合,还是有一定必要的,比如将Web Service同Spring MVC整合在一起,让Web Service调用Spring MVC的某些功能。

    用IDEA创建Spring MVC工程。调整后的结构如下(忽略target)。

    将Web Service整合到Spring MVC中就要保证二者均能正常工作,同时可以交互(比如Web Service调用Spring MVC里的功能)。

    列出一些关键点:

      1. spring-mvc-servlet.xml

     1 <context:component-scan base-package="com.cnblogs.helloyz"/>
     2 
     3        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     4               <property name="prefix" value="/WEB-INF/pages/"/>
     5               <property name="suffix" value=".jsp"/>
     6        </bean>
     7 
     8        <bean id="jaxWsServiceExporter" class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
     9               <property name="baseAddress" value="http://localhost:9000/services/"/>
    10        </bean>

      在这个xml里添加SimpleJaxWsServiceExporter,用于发布Web Service。

      2. HelloWorld.java

    @Component
    @WebService(serviceName = "hello-service")
    public class HelloWorld {
    
      @Autowired
      private HelloController helloController;
    
      @WebMethod
      public String sayHelloWorldFrom(String from) {
        String result = "Hello, world, from " + from;
        System.out.println(result);
        return result;
      }
    
      @WebMethod
      public String sayHello(String from, String to) {
        String result = from + ":"Hello", " + to;
        System.out.println(result);
        System.out.println("------------------");
        helloController.prepareForWebService();
        return result;
      }
    
    }

    所有WebService必须由Spring容器管理,也才能经SimpleJaxWsServiceExporter对外发布。

    此外,注入helloController来验证Web Service同Spring MVC的交互。

    启动Tomcat进行验证。

    1. Spring MVC功能正常。

    2. 访问http://localhost:9000/services/hello-service?wsdl,Web Service发布成功。

    3. 同Part 1,编写client端代码。

    Client.java
    public class Client {
        public static void main(String[] args) {
            HelloService helloService = new HelloService();
            HelloWorld helloWorld = helloService.getHelloWorldPort();
            String result = helloWorld.sayHello("Mars", "Earth");
            System.out.println(result);
            System.out.println("-------------");
            result = helloWorld.sayHelloWorldFrom("Mars");
            System.out.println(result);
        }
    }

    运行main,结果如下: 

         

    Web Service功能正常;Web Service调用Spring MVC功能成功。

    Source Code get here

    Done!

  • 相关阅读:
    Android 使用WebView显示网页
    Android 使用ProgressBar实现进度条
    Android 使用Spinner实现下拉列表
    Android 使用GridView以表格的形式显示多张图片
    Android 使用DatePicker以及TimePicker显示当前日期和时间
    Android 使用ListView显示信息列表
    IIS配置步骤,绝对有用,百度上的不全面,是百度的补充
    冒烟测试
    广度优先和深度优先
    排序算法二(时间复杂度为O(N*logN))
  • 原文地址:https://www.cnblogs.com/hello-yz/p/6054938.html
Copyright © 2011-2022 走看看