首页 > 解决方案 > 带有 json 数据的 Java 请求

问题描述

我想用 Java 发出 HTTP 请求,但我是 Java 新手,不知道怎么做。我看过一些教程,但我无法理解任何东西。我想发送 JSON 数据并接收 JSON 数据。在 Python 中,它看起来像这样:

response = json.load(urllib.request.urlopen(urllib.request.Request('http://localhost:8765', requestJson)))

任何帮助将非常感激。

标签: java

解决方案


使用以下链接作为参考 https://www.baeldung.com/java-http-request

如果您使用外部库,您也可以使用 rest 模板。

ResponseEntity<> response = restTemplate.exchange(
                    UriComponentsBuilder.fromHttpUrl(baseurl + "jobs").toString(),
                    HttpMethod.POST,
                    new HttpEntity<>(body,headers),
                    <someClass>.class);

推荐阅读