如何在 Spring Boot 中发送带有 header、params 和 body 的 post Restful api 作为 Rest 模板

发布于 2025-01-09 16:15:39 字数 93 浏览 0 评论 0原文

我需要使用需要 header、body 和 params 的 Restful api。

有什么方法可以将参数、正文和标头与resttemplate一起发送吗?

I need to consume restful api that required header, body, and params.

Are there any way to send params, body, and headers together with resttemplate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み格子的夏天 2025-01-16 16:15:39

您可以尝试Webclient将消息发布到特定的e/p,
参考 - https://medium.com/p/9863094bac3
对于前

UriCimponenets uriComponents =UriComponentsBuilds.fromHttpUrl(“http://endpoint/{param}")
 .buildAndExpand(inputParam);
HttpHeaders headers= new HttpHeaders ();
Webclient webclient;
//post response status of operation .
String response=webclient.method(HttpMethod.POST)
      .uri (uriComponents.toUri())
      .headers(httpHeaders-> {
               httpHeaders.add(“key”,”value”);
                })
      .contentType(MediaType.APPLICATION_JSON)
      .body(BodyInserters.fromValue(<jsonStringBody>))
      .exchange()
      .flatMap(clientResponse -> {
           if(clientResponse.statusCode().is4xxClientError())
              {
             clientResponse->             {clientResponse.bodyToMono(String.class).subscriber(errorBody->log.error(error.Body));
    return Mono.Error(new                  ResponseStatusException(clientResponse.statusCode()));
             }
           else 
             return clientResponse.bodyToMono(String.class);                           })
      .doOnSuccess(clientResposne -> { log.info(“Response:       {}”,clientResponse)})
      .block(Duration,ofSeconds(50));

You can try Webclient to post message to specific e/p,
ref - https://medium.com/p/9863094bac3
for ex-

UriCimponenets uriComponents =UriComponentsBuilds.fromHttpUrl(“http://endpoint/{param}")
 .buildAndExpand(inputParam);
HttpHeaders headers= new HttpHeaders ();
Webclient webclient;
//post response status of operation .
String response=webclient.method(HttpMethod.POST)
      .uri (uriComponents.toUri())
      .headers(httpHeaders-> {
               httpHeaders.add(“key”,”value”);
                })
      .contentType(MediaType.APPLICATION_JSON)
      .body(BodyInserters.fromValue(<jsonStringBody>))
      .exchange()
      .flatMap(clientResponse -> {
           if(clientResponse.statusCode().is4xxClientError())
              {
             clientResponse->             {clientResponse.bodyToMono(String.class).subscriber(errorBody->log.error(error.Body));
    return Mono.Error(new                  ResponseStatusException(clientResponse.statusCode()));
             }
           else 
             return clientResponse.bodyToMono(String.class);                           })
      .doOnSuccess(clientResposne -> { log.info(“Response:       {}”,clientResponse)})
      .block(Duration,ofSeconds(50));
孤千羽 2025-01-16 16:15:39

这是使用 RestTemplate 发送 Post 请求的简单示例:

// pretend RestTemplate already initialized

String url = "your url";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION.JSON); // whatever u want
headers.set("KEY", "VALUE");

HttpEntity requestHeader = new HttpEntity(headers);

// set url, header, and Type of value to be returned from the request
ResponseEntity<?> response = restTemplate.postForEntity(url, requestHeader, String.class); 

This is a simple example of send Post request using RestTemplate:

// pretend RestTemplate already initialized

String url = "your url";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION.JSON); // whatever u want
headers.set("KEY", "VALUE");

HttpEntity requestHeader = new HttpEntity(headers);

// set url, header, and Type of value to be returned from the request
ResponseEntity<?> response = restTemplate.postForEntity(url, requestHeader, String.class); 

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文