Basic usage of the RESTful protocol

更新时间:
复制 MD 格式

In SOFARPC, each communication protocol requires a specific binding. To use the RESTful protocol, you must set the binding to REST.

Publish a service

  1. Define the service interface.

    To define a RESTful service interface, you must add metadata to the interface using standard Java API for RESTful Web Services (JAX-RS) annotations. For example:

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

    For more information about how to use standard JAX-RS annotations, see the RESTEasy documentation.

  2. Publish the service.

    After you define the interface, you can publish its implementation as a service. The following example shows how to publish a service using annotations:

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

For information about other ways to publish the service, see Basic usage of the BOLT protocol.

Access a service from a browser

After a service is published, you can access it directly from a browser. The default port for SOFARPC RESTful services is 8341. For the service published in the preceding example, the endpoint is as follows:

http://localhost:8341/sample/hello

Reference a service

You can also reference a service using the standard SOFARPC service reference. The following example shows how to reference a service using an annotation:

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

For information about other ways to reference the service, see Basic usage of the BOLT protocol.