How to create a client for a spring boot service?

自闭症网瘾萝莉.ら 提交于 2020-01-13 07:24:08

问题


I have a simple CRUD API for a Spring Boot service based on this example.

The API is provided with the following definition:

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

From a curl client, I can get the list of users using the following implicit GET command: curl localhost:8080/persons.

From Java's perspective we just called the remote equivalent of Iterable<Person> persons = personRepository.findAll().

The question is whether spring can auto-create a remote implementation (client) of the same API without the need to manually type paths or know the method type (like needed when using RestTemplate)?

Example of an such a client usage:

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

How can I get to such a client "generated" implementation"?

* I know that passing the URL directly contradicts the service discovery architecture, but it's just for the sake of understanding how to get to a well defined client. The same client can be used within a service discovery architecture, by discovering the URL instead of manually setting it.


回答1:


Try Feign Client. It provides autogeneration of HTTP request handlers for your Java interfaces. To make it work, you need to include Spring Cloud, standard Spring framework doesn't have this feature.



来源:https://stackoverflow.com/questions/47682312/how-to-create-a-client-for-a-spring-boot-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!