zoukankan      html  css  js  c++  java
  • dubbox rest服务

    1.xml配置: web.xml

    定义监听的contextpath,见rest协议定义

    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

    2.xml配置:dubbo-demo-provider.xml

    ##rest协议定义:
    ##port:和服务器端口保持一致
    ##contextpath:和web服务器web.xml定义保持一致
    ##server:默认servlet,在没有在web服务器中运行的时候,可以写tomcat等由应用启动一个web服务器
    <dubbo:protocol name="rest" port="8888" contextpath="services" server="servlet"/>
    
    
    ##远程服务定义
    ##interface : 服务接口    
    ##ref:服务实际实现的service,引用其它定义的spring bean
    ##protocol:服务的协议模式
    <dubbo:service interface="com.alibaba.dubbo.demo.user.facade.UserRestService" ref="userRestService"  protocol="rest" validation="true"/>
    
    
    ##实际服务实现
    <bean id="userRestService" class="com.alibaba.dubbo.demo.user.facade.UserRestServiceImpl">
        <property name="userService" ref="userService"/>
    </bean>

    3.java代码:UserRestServiceImpl

    class定义:

    ##@Path:整体服务的访问路径 
        本样例http访问就应该是:  http:/IP:PORT/应用名称/contextpath/users
        contextpath:web.xml和resf服务中定义的
    ##可以接受的数据格式。json和简单xml
    @Path("users")
    @Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
    @Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
    public class UserRestServiceImpl implements UserRestService {

    Method定义:

    ##@POST:http接受的方法,可以GET,POST,delete等
        本样例http访问就应该是:  
            http:/IP:PORT/应用名称/contextpath/users/register.json
            http:/IP:PORT/应用名称/contextpath/users/register.xml
    ##@Path:服务路径
    @POST
    @Path("register")
    public RegistrationResult registerUser(User user) {
        return new RegistrationResult(userService.registerUser(user));
    }

    客户端:

    见RestClient.java

    js调用

    <!DOCTYPE html>
    <html>
    <head>
        <script src="jquery-1.10.2.min.js">
        </script>
        <script>
            $(document).ready(function(){
                $("#btn1").click(function(){
                    $('#test').load('http://127.0.0.1:8888/services/u/1.json');
                })
            })
        </script>
    </head>
    
    <body>
    
    <h3 id="test">请点击下面的按钮,通过 jQuery AJAX 改变这段文本。</h3>
    <button id="btn1" type="button">获得外部的内容</button>
    
    </body>
    </html>

    参考:http://www.ganps.net/dubboxie-yi/

    http://dangdangdotcom.github.io/dubbox/rest.html

  • 相关阅读:
    封装简单的mvc框架
    php中date函数获取当前时间的时区误差解决办法
    PHP中date函数参数详解
    PHP中字符串补齐为定长
    php将xml文件转化为数组:simplexml_load_string
    PHP基于变量的引用实现的树状结构
    EcShop后台添加菜单[步骤]
    Cmd批处理语法实例
    Mysql语句的批量操作[修改]
    HTML前端技术(JS的使用,包括数组和字符串)
  • 原文地址:https://www.cnblogs.com/Gyoung/p/5919205.html
Copyright © 2011-2022 走看看