如何为Spring启动服务创建客户端?

时间:2017-12-06 19:46:08

标签: java spring spring-boot crud

我有一个基于this example的Spring Boot服务的简单CRUD API。

API提供以下定义:

@RepositoryRestResource
public interface PersonRepository extends CrudRepository<Person, Long> {}

从curl客户端,我可以使用以下隐式GET命令获取用户列表:curl localhost:8080/persons

从Java的角度来看,我们只称远程等效于Iterable<Person> persons = personRepository.findAll()

问题是spring是否可以自动创建相同API 的远程实现(客户端),而无需需要手动键入路径或知道方法类型(如使用RestTemplate)时需要吗?

此类客户端使用示例:

PersonRepository personRepository = new PersonRepositoryClient(http://localhost:8080");
Iterable<Person> persons = personRepository.findAll(); // findAll does a remote call as if it was local.

我怎样才能找到这样一个客户&#34;生成&#34;实施&#34;

*我知道传递URL直接与服务发现架构相矛盾,但它只是为了理解如何到达定义良好的客户端。通过发现URL而不是手动设置URL,可以在服务发现体系结构中使用同一客户端。

1 个答案:

答案 0 :(得分:0)

试试Feign Client。它为您的Java接口提供HTTP请求处理程序的自动生成。要使其工作,您需要包含Spring Cloud,标准Spring框架不具备此功能。

相关问题