gpt4 book ai didi

带有标题和正文的 Spring RestTemplate POST 查询

转载 作者:行者123 更新时间:2023-12-03 22:47:51 24 4
gpt4 key购买 nike

我需要使用给定的 API 定义,但我无法在 documentation 处找到同时接受 header 和请求正文的函数调用.请建议在这里使用 RestTemplate 的哪个功能。

@RequestMapping(value = "/createObject", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<CreateObjectOutput> createObject(
@RequestBody CreateObjectInput req)
{
CreateObjectOutput out = new CreateObjectOutput();
///// Some Code
return new ResponseEntity<CreateObjectOutput>(out, HttpStatus.OK);
}

最佳答案

RestTemplate template = new RestTemplate();
CreateObjectInput payload = new CreateObjectInput();

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<CreateObjectOutput> requestEntity =
new HttpEntity<>(payload, headers);
CreateObjectOutput response =
template.exchange("url", HttpMethod.POST, requestEntity,
CreateObjectOutput.class);

关于带有标题和正文的 Spring RestTemplate POST 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49397777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com