我拥有一个Spring boot Rest API,它被许多外部应用程序使用。该接口在一次请求中接收到一个文档列表(base64字符串)。每个文档的大小约为100MB,大多数情况下请求负载中有6,7个文档。假设列表中有7个文档,它将消耗700MB的内存,这太多了。我想让它的内存效率更高。我不能要求API的Consumer逐个发送文档,我必须在请求中一次接收它们,但我希望将文档逐个加载到内存中,而不是一次性加载。下面是一个例子。
@PostMapping("/documents")
Employee newEmployee(@RequestBody List<String> DocuemntsInBase64) {
} 另外,让我知道JsonSurfer是否可以帮助救援。
发布于 2021-01-12 20:32:10
嘿,我已经通过使用HttpServletRequest和JsonSurfer库解开了这个谜团。下面是一个示例代码:
@RequestMapping(value = "/postDocuments",
method = RequestMethod.POST,
consumes = {"application/json", "application/xml"},
produces = {"application/json", "application/xml"})
@ResponseStatus(HttpStatus.CREATED)
public void createHotel(HttpServletRequest request, HttpServletResponse response) {
JsonSurfer surfer = JsonSurferJackson.INSTANCE;
try {
surfer.configBuilder()
.bind("Your JsonPath of list", new JsonPathListener() {
@Override
public void onValue(Object value, ParsingContext context) {
logger.info("Document of size: "+value.toString().getBytes().length);
}
})
.buildAndSurf(request.getReader());
} catch (IOException e) {
e.printStackTrace();
}
}https://stackoverflow.com/questions/65561173
复制相似问题