文档

RESTful 协议的基本用法

更新时间:

在 SOFARPC 中,使用不同的通信协议即使用不同的 Binding。如果需要使用 RESTful 协议,只要将 Binding 设置为 REST 即可。

发布服务

  1. 定义服务接口。

    在定义 RESTful 服务接口时,需要采用 JAXRS 标准的注解在接口上加上元信息,示例如下:

    @Path("sample")
        public interface SampleService{
            @GET
            @Path("hello")
            String hello();
        }
    说明

    JAXRS 的标准的注解的使用方式可以参考 RESTEasy 的文档

  2. 发布服务。

    定义好接口之后,你可以将接口的实现发布成一个服务。以 Annotation 方式发布服务为例:

    @Service
    @SofaService(bindings ={@SofaServiceBinding(bindingType ="rest")})
    public class RestfulSampleServiceImpl implements SampleService{
        @Override
        public String hello(){
            return "Hello";
        }
    }

如果您要通过其他方式发布服务,请参考 BOLT 协议基本使用

通过浏览器访问服务

服务发布后,您可以通过浏览器直接访问服务。以上文发布的服务为例,SOFARPC 的 RESTful 服务的默认端口为 8341,访问地址如下:

http://localhost:8341/sample/hello

引用服务

您也可以通过 SOFARPC 标准的服务引用的方式来引用服务。以 Annotation 方式引用服务为例:

@SofaReference(binding =@SofaReferenceBinding(bindingType ="rest"))
private SampleService sampleService;

如果您要通过其他方式引用服务,请参考 BOLT 协议基本使用

  • 本页导读 (0)
文档反馈