在 Spring 中发出 HTTP 请求的最简单方法

发布于 2024-12-13 09:17:07 字数 270 浏览 5 评论 0原文

在我的 Spring Web 应用程序中,我需要向非 RESTful API 发出 HTTP 请求,并将响应正文解析回字符串(它是一个单维 CSV 列表)。

我之前使用过 RestTemplate,但这不是 RESTful,并且不能很好地映射到类。每当我“手动”实现类似的东西(例如使用HttpClient)时,我总是会发现Spring有一个实用程序类,可以使事情变得更加简单。

Spring 中有什么东西可以“开箱即用”地完成这项工作吗?

In my Spring web application I need to make an HTTP request to a non-RESTful API, and parse the response body back as a String (it's a single-dimension CSV list).

I've used RestTemplate before, but this isn't RESTful and doesn't map nicely on to classes. Whenever I implement something like this 'by hand' (eg using HttpClient) I invariably find out later that Spring has a utility class that makes things much simpler.

Is there anything in Spring that will do this job 'out of the box'?

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

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

发布评论

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

评论(2

傾城如夢未必闌珊 2024-12-20 09:17:07

如果您查看 RestTemplate 的源代码,您会发现它内部使用的

java.net.URL

url.openConnection()

Java 中进行 HTTP 调用的标准方式,因此您可以安全地使用它。如果 Spring 中有一个“HTTP 客户端”实用程序,那么 RestTemplate 也会使用它。

If you look at the source of RestTemplate you will find that internally it uses

java.net.URL

and

url.openConnection()

that is the standard way in Java to make HTTP calls, so you are safe to use that. If there would be a "HTTP client" utility in spring then the RestTemplate would use that too.

忆依然 2024-12-20 09:17:07

我使用 Spring Boot 和 Spring 4.3 Core,发现了一种非常简单的方法来使用 OkHttpClient 发出 Http 请求并读取响应。这是代码

Request request = new Request.Builder().method("PUT", "some your request body")
            .url(YOUR_URL)
            .build();
        OkHttpClient httpClient = new OkHttpClient();
        try
        {
            Response response = httpClient.newBuilder()
            .readTimeout(1, TimeUnit.SECONDS)
            .build()
            .newCall(request)
            .execute();
            if(response.isSuccessful())
            {
                // notification about succesful request
            }
            else
            {
                // notification about failure request
            }
        }
        catch (IOException e1)
        {
            // notification about other problems
        }

I use the Spring Boot with Spring 4.3 Core inside and found a very simple way to make Http request and read responses by using OkHttpClient. Here is the code

Request request = new Request.Builder().method("PUT", "some your request body")
            .url(YOUR_URL)
            .build();
        OkHttpClient httpClient = new OkHttpClient();
        try
        {
            Response response = httpClient.newBuilder()
            .readTimeout(1, TimeUnit.SECONDS)
            .build()
            .newCall(request)
            .execute();
            if(response.isSuccessful())
            {
                // notification about succesful request
            }
            else
            {
                // notification about failure request
            }
        }
        catch (IOException e1)
        {
            // notification about other problems
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文