In SOFARPC, each communication protocol requires a specific binding. To use the RESTful protocol, you must set the binding to REST.
Publish a service
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(); }NoteFor more information about how to use standard JAX-RS annotations, see the RESTEasy documentation.
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/helloReference 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.