问题
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