更新时间:2021-01-11 14:26
在 SOFARPC 中,使用不同的通信协议即使用不同的 Binding。如果需要使用 RESTful 协议,只要将 Binding 设置为 REST 即可。
在定义 RESTful 服务接口的时候,需要采用 JAXRS 标准的注解在接口上加上元信息,比如下面的接口:
@Path("sample")
public interface SampleService {
@GET
@Path("hello")
String hello();
}
在定义好接口之后,将接口的实现发布成一个服务。例如,通过 Annotation 的方式发布任务:
@Service
@SofaService(bindings = {@SofaServiceBinding(bindingType = "rest")})
public class RestfulSampleServiceImpl implements SampleService {
@Override
public String hello() {
return "Hello";
}
}
如果要通过其他的方式发布服务,请参考 Bolt 协议基本使用。
在发布服务之后,用户可以通过浏览器来直接访问服务,对于上面的服务,访问的地址如下:
http://localhost:8341/sample/hello
SOFARPC 的 RESTful 服务的默认端口为 8341。
除了通过浏览器访问 SOFARPC 发布的 RESTful 服务之外,用户也可以通过 SOFARPC 标准的服务引用的方式来引用服务,比如通过 Annotation 的方式:
@SofaReference(binding = @SofaReferenceBinding(bindingType = "rest"))
private SampleService sampleService;
如果要使用其他的方式引用服务,请参考 Bolt 协议基本使用。
在文档使用中是否遇到以下问题
更多建议
匿名提交