如何为Spring Boot服务创建客户端? [英] How to create a client for a spring boot service?

查看:161
本文介绍了如何为Spring Boot服务创建客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于的Spring Boot服务的简单CRUD API。此示例

API提供了以下定义:

The API is provided with the following definition:

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

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

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

来自Java从这个角度来看,我们仅将 Iterable< Person> person = personRepository.findAll()

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

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

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"?

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

* 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.

推荐答案

尝试虚假客户端。它为Java接口自动生成HTTP请求处理程序。要使其正常运行,您需要包括Spring Cloud,标准的Spring框架没有此功能。

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.

这篇关于如何为Spring Boot服务创建客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆