【发布时间】:2021-10-14 04:45:30
【问题描述】:
我正在尝试使用 ResponseEntity 在 Spring Boot 中将数据从一台服务器 (8081) 共享到另一台服务器 (8082),但我无法构建主体。
这是我写的代码,
服务器 1-
@GetMapping("/redirect")
public ResponseEntity<Void> redirectPgServer(@RequestParam("txnId") String txnId,
@RequestParam("amount") String amount) {
// Redirect to server 2
ProductDetails ref=new ProductDetails();
ref.setTxnId("txnId");
ref.setAmount("amount);
HttpHeaders head = new HttpHeaders();
String url = "http://localhost:8082/redirect-server";
System.out.println(url);
head.setLocation(URI.create(url));
return new ResponseEntity<Void>(ref, head, HttpStatus.FOUND);
}
服务器2-
@GetMapping(value = "/redirect-server")
public String validateData(@RequestBody ProductDetails ref)
{
//Server 2 receive data
System.out.println(ref.getAmount());
//Does not gives any output since it throws error Request Body Missing, also if I mention
post mapping above it throws error Request method 'GET' not supported]
return "index"; //Using Thymeleaf for displaying html page.
}
我可以通过操纵 url 和使用 PathVariable 仅使用 header 共享数据,但我想使用 Post Mapping 因为我想隐藏参数在浏览器中对用户可见的 URL。我还尝试使用 RestTemplate 将我带回服务器 1 并引发错误 - Spring rest 模板无法从 text/html utf8 转换。
【问题讨论】:
-
这个问题不完整。这里没有通讯。只有一个定义的端点,另一个似乎什么都不做的端点。缺少的部分很可能是您遇到问题的地方。
-
您很可能需要将
ResponseEntity<Void>更改为ResponseEntity<ProductDetails>
标签: java spring spring-boot spring-mvc