java如何调用python接口_rest命令

java如何调用python接口_rest命令在 Java 中调用 REST API 接口进行测试 你可以使用以下几种方法 方法一 使用 HttpURLConne javaimport java io BufferedRead import java io InputStreamR import java net HttpURLConne import java net URL public class

在Java中调用REST API接口进行测试,你可以使用以下几种方法:

方法一:使用HttpURLConnection

java

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class RestClient {

public static void main(String[] args) {

try {

URL url = new URL("http://example.com/api/resource");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setRequestProperty("Content-Type", "application/json");

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String inputLine;

StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {

response.append(inputLine);

}

in.close();

System.out.println(response.toString());

} catch (Exception e) {

e.printStackTrace();

}

}

}

方法二:使用Rest-assured

如果你使用Maven,需要在`pom.xml`中添加Rest-assured依赖:

xml

io.rest-assured

rest-assured

5.3.0

test

然后你可以使用以下代码进行测试:

java

import static io.restassured.RestAssured.*;

import static org.hamcrest.Matchers.*;

import org.junit.Test;

public class SimpleApiTest {

@Test

public void testGetRequest() {

given()

.when()

.get("https://api.example.com/users/1")

.then()

.statusCode(200)

.body("name", equalTo("张三"))

.body("age", 25);

}

}

方法三:使用HttpClient

如果你需要更复杂的请求,如PUT或DELETE,可以使用HttpClient:

java

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

public class RestUtil {

public static void postMethod(String url, String query) throws IOException {

CloseableHttpClient client = HttpClients.createDefault();

HttpPost post = new HttpPost(url);

post.setHeader("Accept", "application/json");

post.setHeader("Content-Type", "application/json");

post.setEntity(new StringEntity(query));

HttpResponse response = client.execute(post);

HttpEntity entity = response.getEntity();

if (entity != null) {

String result = EntityUtils.toString(entity);

System.out.println(result);

}

}

}

方法四:使用JIRA REST API

对于JIRA REST API,你可以使用以下代码:

java

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

public class JiraRestClient {

public static void postMethod(String url, String query) throws IOException {

CloseableHttpClient client = HttpClients.createDefault();

HttpPost post = new HttpPost(url);

post.setHeader("Accept", "application/json");

post.setHeader("Content-Type", "application/json");

post.setEntity(new StringEntity(query));

HttpResponse response = client.execute(post);

HttpEntity entity = response.getEntity();

if (entity != null) {

String result = EntityUtils.toString(entity);

System.out.println(result);

}

}

}

请根据你的具体需求

编程小号
上一篇 2025-06-05 10:49
下一篇 2026-05-17 23:53

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://sigusoft.com/bj/39800.html