java - 从文件中获取 Json 以在 Java 中的 RESTful Post 请求中使用

标签 java json rest

我见过几个人们获取字符串并将其用作 Json 的示例。我想从其中读取 json 的文件,并将其用作我的请求正文。最有效的方法是什么?

非常感谢您的帮助。我的最终解决方案是使用 groovy 控制台并从 .json 文件读取,如下所示:

@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.2.3')
@Grab(group='org.apache.httpcomponents', module='httpcore', version='4.2.3')
@Grab(group='org.apache.commons', module='commons-io', version='1.3.2')
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.methods.HttpPost
import org.apache.http.HttpResponse
import org.apache.http.HttpEntity
import org.apache.http.entity.StringEntity
import org.apache.http.util.EntityUtils
import org.apache.commons.io.IOUtils

String json = IOUtils.toString(new FileInputStream("C:\\MyHome\\example.json"));

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://api/location/send");
httpPost.addHeader("content-type", "application/json");
httpPost.setEntity(new StringEntity(json));
HttpResponse response2 = httpclient.execute(httpPost);

try {
    System.out.println(response2.getStatusLine());
    HttpEntity entity2 = response2.getEntity();
    // do something useful with the response body
    // and ensure it is fully consumed
    EntityUtils.consume(entity2);
} finally {
    httpPost.releaseConnection();
}

这对我来说是一次很好的快速健全性检查,是快速制作原型(prototype)以了解我正在尝试做的事情的大局的好方法。再次感谢。

最佳答案

您可以使用 Apache HttpComponents 。这是一个小示例,您可以在 Groovy 附带的 GroovyConsole 中试用。我使用它是因为它是快速原型(prototype)化的最简单方法,因为可以使用 Grape 自动加载库 jar(这就是 @Grab 注释的作用)。此外,在 GroovyConsole 中,无需创建项目。您也不需要使用 Groovy,尽管我通常会使用。

请注意,下面的代码是取自 HttpClient Quick Start 的修改后的 POST 示例。 。另请注意,HttpComponents/HttpClient 是一个较新的项目,它取代了 Apache 中较旧的 HttpClient(只需清除此项目,以防您通过 Google 搜索并看到没有 HttpComponents 的 HttpClient)。我使用的主机(posttestserver.com)只是一个测试服务器,它将接受 Http 请求,并在一切正常的情况下返回响应。

@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.2.3')
@Grab(group='org.apache.httpcomponents', module='httpcore', version='4.2.3')
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.methods.HttpPost
import org.apache.http.HttpResponse
import org.apache.http.HttpEntity
import org.apache.http.entity.StringEntity
import org.apache.http.util.EntityUtils


String json = "{foo: 123, bar: \"hello\"}";

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://posttestserver.com/post.php");
httpPost.setEntity(new StringEntity(json));
HttpResponse response2 = httpclient.execute(httpPost);

try {
    System.out.println(response2.getStatusLine());
    HttpEntity entity2 = response2.getEntity();
    // do something useful with the response body
    // and ensure it is fully consumed
    EntityUtils.consume(entity2);
} finally {
    httpPost.releaseConnection();
}

关于java - 从文件中获取 Json 以在 Java 中的 RESTful Post 请求中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15300811/

相关文章:

java - 一行状态检查中的三个状态? (关于Java语法)

json - 使用jquery从API获取数据

asp.net - 表单中的表单和 JSON 调用

java - 在 Retrofit 中将参数添加到 url 的末尾

.net - RestSharp 序列化为 JSON,对象未按预期使用 SerializeAs 属性

java - 在 Java 中将字符串转换为 SomeType

java - 如何从没有主键的连接表中创建实体

java - Vert.x - 当客户端断开连接时停止分块响应

node.js - Node/Express RESTful 请求中存在多个参数?

SQL Server 表转 json