【问题标题】:Communicate two form based springboot application using rest使用rest通信两个基于表单的springboot应用程序
【发布时间】:2019-07-18 09:22:11
【问题描述】:

我有两个 Springboot 应用程序。一是加法,二是减法。我只在 html 中创建了一种用于加法和减法的表单。

我想要的是当我点击减法时,它应该调用减法逻辑并使用 REST 对来自 html 的两个值进行减法。

【问题讨论】:

  • 您真的有两个独立的后端应用程序吗?还是一个应用程序具有两个不同的 API 端点?

标签: java rest spring-boot spring-mvc


【解决方案1】:

你可以使用RestTemplate类。

String exampleYourUrl = "http://localhost:8080/calculate/subtract";

UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(exampleYourUrl)
            .queryParam("param1", "12")
            .queryParam("param2", "3");
RestTemplate restTemplate = new RestTemplate();
Integer response = restTemplate.getForObject(uriBuilder.toUriString(), Integer.class);

【讨论】:

  • 假设 OP 使用 Spring 5+,新的WebClient 是更好的选择。 RestTemplate 将被弃用。
  • 它甚至在您提供的链接中这样说:“RestTemplate 将在未来的版本中被弃用,并且不会增加主要的新功能。”
【解决方案2】:

我有两个 springboot 应用程序

您是否考虑过使用 RestTemplate 进行休息调用/内部通信?

让我知道:)我会编辑同样的内容。

已编辑:

RestTemplate restTemplate = new RestTemplate();
HttpEntity<ObjectContainingBothP> httpEntity = new HttpEntity<ObjectContainingBothParams>(ObjectContainingBothParams);
final String urlofanothermethodinanotherspringboot=//whole url;
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(urlofanothermethodinanotherspringboot);
ResponseEntity<ResponseDto> exchange = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST,
                httpEntity, response.class//reponse that you want back);
Response response = exchange.getBody();

虽然你可以使用 WebClient 代替 RestTemplate,但这完全取决于需求:

RestTemplate:-同步和阻塞,在响应返回之前,您无法继续进行。 WebClient:-你不需要等待。

考虑到你的情况,我认为 Rest Template 是:)。

【讨论】:

  • 是的,我用休息来进行相互交流,还有一件事,我用百里香形式。
猜你喜欢
  • 2019-08-06
  • 2017-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-21
相关资源
最近更新 更多