All Products
Search
Document Center

RESTful Basics

Last Updated: Aug 17, 2020

To use a different communication protocol in SOFARPC, you need to set Binding to a different value. To use the RESTful protocol, set Binding to REST.

Publish a service

  • Interface: When customizing a RESTful service interface, you need to use an annotation of the JAXRS standard to add metadata to the interface. For example:

    1. @Path("sample")
    2. public interface SampleService {
    3. @GET
    4. @Path("hello")
    5. String hello();
    6. }

    Note: For more information about how to use annotations of the JAXRS standard, see the RESTEasy document.

  • Implementation: After defining the interface, you need to publish the interface implementation as a service. For example, publish a task in the Annotation mode:

    1. @Service
    2. @SofaService(bindings = {@SofaServiceBinding(bindingType = "rest")})
    3. public class RestfulSampleServiceImpl implements SampleService {
    4. @Override
    5. public String hello() {
    6. return "Hello";
    7. }
    8. }

    To publish a service in other modes, see Basic usage of Bolt protocol.

Reference services

  • Annotation: Apart from accessing the RESTful service published in SOFARPC through a browser, you can also reference the service by using standard service reference modes, such as the Annotation mode, in SOFARPC:
    1. @SofaReference(binding = @SofaReferenceBinding(bindingType = "rest"))
    2. private SampleService sampleService;
  • Other configuration: For more information about how to reference a service with other configuration, see Basic usage of Bolt protocol.

Access services

After a service is published, users can access the service through a browser. The URL for the above service is:

  1. http://localhost:8341/sample/hello

The default port for the RESTful service in SOFARPC is 8341.