【发布时间】:2019-07-18 09:22:11
【问题描述】:
我有两个 Springboot 应用程序。一是加法,二是减法。我只在 html 中创建了一种用于加法和减法的表单。
我想要的是当我点击减法时,它应该调用减法逻辑并使用 REST 对来自 html 的两个值进行减法。
【问题讨论】:
-
您真的有两个独立的后端应用程序吗?还是一个应用程序具有两个不同的 API 端点?
标签: java rest spring-boot spring-mvc
我有两个 Springboot 应用程序。一是加法,二是减法。我只在 html 中创建了一种用于加法和减法的表单。
我想要的是当我点击减法时,它应该调用减法逻辑并使用 REST 对来自 html 的两个值进行减法。
【问题讨论】:
标签: java rest spring-boot spring-mvc
你可以使用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);
【讨论】:
WebClient 是更好的选择。 RestTemplate 将被弃用。
我有两个 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 是:)。
【讨论】: