如何使用Java查找响应负载(JSON)大小

huangapple go评论75阅读模式
英文:

How to find the response payload (JSON) size using java

问题

Sure, here's the translated text:

我想使用Java来确定REST API的JSON响应负载大小。为了进一步描述,我必须设计一个批量API,该API正在从应用程序中导出大量数据,分页和安全性已经就位,但我想限制单个页面的大小,以确保服务的连续性和性能输出。

在这方面,感谢您的帮助 提前致谢!

英文:

I want to determine JSON response payload size for REST API using Java. To describe further I have to design a bulk API that is exporting large data from application, pagination and security is in place but I want to limit the individual page size to ensure the service continuity and performance output.

your help in this regards is appreciated Thanks in advance!

答案1

得分: 2

你可能需要将其实际编码为JSON,以便能够计算大小。数据模型可能无法准确指示通过JSON公开了哪些属性,它们的名称是什么,或者它们是如何编码的。

有一些其他问题在SO上询问了类似的事情,可以参考
https://stackoverflow.com/questions/39047624/is-there-an-easy-way-to-estimate-size-of-a-json-object
https://stackoverflow.com/questions/54042263/calculate-size-in-bytes-of-json-payload-including-it-in-the-json-payload-in-php

你没有说明如何使用JSON,但很可能是使用Jackson,也可能是Spring,所以像这样的东西应该可以将你放在基本的范围内

ObjectMapper om = new ObjectMapper();
Object example = ... // 你想要找到JSON大小的对象

String jsonString = om.writeValueAsString(example); // 获取JSON字符串
int characterCount = json.length(); // 计算字符数

byte[] bytes = om.writeValueAsBytes(example); // 获取JSON作为UTF-8字节
int byteCount = bytes.length; // 计算字节数

characterCountbyteCount 之间的区别可能不明显,特别是如果你不习惯处理Unicode代码点。HTTP的Content-Length表示八位字节的数量,所以byteCount会更准确,参见https://stackoverflow.com/questions/2773396/whats-the-content-length-field-in-http-header。

英文:

You're likely to need to actually encode it to JSON to be able to calculate that. The data model may not accurately indicate exactly what properties are exposed via JSON, what they're called, or how they're encoded.

There are a few other questions on SO asking roughly the same thing, see
https://stackoverflow.com/questions/39047624/is-there-an-easy-way-to-estimate-size-of-a-json-object and
https://stackoverflow.com/questions/54042263/calculate-size-in-bytes-of-json-payload-including-it-in-the-json-payload-in-php

You don't say how you're using JSON, but it's fairly likely it's using Jackson, and perhaps Spring, so something like this ought to put you in the basic ballpark

ObjectMapper om = new ObjectMapper();
Object example = ... // the object you want to find the JSON size for

String jsonString = om.writeValueAsString(example); // get the JSON string
int characterCount = json.length(); // count the number of characters

byte[] bytes = om.writeValueAsBytes(example); // get the JSON as UTF-8 bytes
int byteCount = bytes.length; // count the number of bytes

The difference between characterCount and byteCount may not be obvious, especially if you're not used to dealing with Unicode code-points. HTTP Content-Length indicates the number of octets (bytes), so byteCount would be most accurate, see https://stackoverflow.com/questions/2773396/whats-the-content-length-field-in-http-header.

答案2

得分: 1

服务器应在响应中设置 Content-Length 头部(参见 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length)。这是载荷的字节大小。

因此,客户端可以像这样读取内容长度:

HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
int length = connection.getContentLength();
英文:

The Server should set the Content-Length header in the response (see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length). This is the size of the payload in bytes.

So, the client can read the content length like this:

HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
int length = connection.getContentLength();

huangapple
  • 本文由 发表于 2020年8月12日 14:07:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63370731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定