欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

dubbo 集成restful协议

程序员文章站 2022-03-08 16:46:21
...

https://blog.csdn.net/lovesomnus/article/details/52179995
https://blog.csdn.net/nauwzj/article/details/18708033

Maven工程请导入以下配置:

<dependency>  
            <groupId>javax.ws.rs</groupId>  
            <artifactId>javax.ws.rs-api</artifactId>  
        </dependency>  

可忽略以下maven配置:

<dependency>  
            <groupId>org.jboss.resteasy</groupId>  
            <artifactId>resteasy-jaxrs</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>org.jboss.resteasy</groupId>  
            <artifactId>resteasy-client</artifactId>  
        </dependency>  
        <dependency>  
           <groupId>javax.el</groupId>  
           <artifactId>javax.el-api</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>org.glassfish.web</groupId>  
            <artifactId>el-impl</artifactId>  
            <scope>provided</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.httpcomponents</groupId>  
            <artifactId>httpcore</artifactId>  
        </dependency>  

1.spring-dubbo-service.xml 如下配置:tomcat

<dubbo:protocol name="rest" port="8888" threads="500" contextpath="user" server="tomcat" accepts="500"  
                    extension="com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter"/>  
<dubbo:service interface="com.sf.sgs.user.core.rest.service.CostomerRestService" ref="costomerRestServiceImpl" protocol="rest" document="customer"></dubbo:service>  

2.定义接口:

package com.sf.sgs.user.core.rest.service;  

import javax.ws.rs.Consumes;  
import javax.ws.rs.POST;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import javax.ws.rs.core.MediaType;  

@Path("customer")  
@Produces({MediaType.APPLICATION_JSON + "; " + MediaType.CHARSET_PARAMETER + "=UTF-8", MediaType.TEXT_XML + "; " + MediaType.CHARSET_PARAMETER + "=UTF-8"})  
public interface CustomerServiceRest {  

    @POST  
    @Path("test")  
    @Consumes(MediaType.APPLICATION_JSON)  
    String test(String name);  
}  

3.访问:
访问URL:http://localhost:8888/user/customer/add

参数值:{“orgCode”:”123ABC”,”source”:1,”name”:”leantaot”,”pinCode”:”123456789123456”,”sex”:”男”,”mobile”:”110”}

相关标签: restfull