【问题标题】:How to send request with Spring mvc?如何使用 Spring mvc 发送请求?
【发布时间】:2020-07-07 22:29:26
【问题描述】:

我正在学习如何制作电报机器人。使用 webhook 电报向您发送带有一些消息的发布请求。 我希望我的服务器获取此请求,处理并将新请求发送到电报获取/发布。 例如: 电报机器人向我发送了一个帖子请求,其中包含聊天中的新消息。 我收到这个请求并处理它。 现在我需要向电报发送新的获取请求以发布类似https://api.telegram.org/bot/sendMessage?chat_id=&text=hello

的消息

有没有办法直接从控制器发送请求?我知道我可以重定向请求,但重定向只能是 GET 并且我需要完全新的请求。

【问题讨论】:

    标签: spring http spring-mvc telegram telegram-bot


    【解决方案1】:
    1. 您可以使用 Spring 的 RestTemplate:

      RestTemplate restTemplate = new RestTemplate();
      
      UriComponentsBuilder telegramRequestBuilder = UriComponentsBuilder.fromHttpUrl("https://api.telegram.org/bot/sendMessage")
              .queryParam("chat_id", 1)
              .queryParam("text", "Hello");
      
      ResponseEntity<String> response
              = restTemplate.getForEntity(telegramRequestBuilder.toUriString(), String.class); // or to a Java pojo class
      
    2. 或者为此使用更新的 Spring WebClient。例如,请参阅this link

    【讨论】:

      【解决方案2】:

      您可以使用 Rest Template 向 Web 服务发送请求。this 是有关如何执行此操作的 spring 指南。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-27
        • 1970-01-01
        • 2016-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-17
        • 1970-01-01
        相关资源
        最近更新 更多