首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring Boot中懒惰地使用Rest控制器中的RequestBody

在Spring Boot中懒惰地使用Rest控制器中的RequestBody
EN

Stack Overflow用户
提问于 2021-01-04 18:34:12
回答 1查看 47关注 0票数 0

我拥有一个Spring boot Rest API,它被许多外部应用程序使用。该接口在一次请求中接收到一个文档列表(base64字符串)。每个文档的大小约为100MB,大多数情况下请求负载中有6,7个文档。假设列表中有7个文档,它将消耗700MB的内存,这太多了。我想让它的内存效率更高。我不能要求API的Consumer逐个发送文档,我必须在请求中一次接收它们,但我希望将文档逐个加载到内存中,而不是一次性加载。下面是一个例子。

代码语言:javascript
复制
  @PostMapping("/documents")
      Employee newEmployee(@RequestBody List<String> DocuemntsInBase64) {
      }  

另外,让我知道JsonSurfer是否可以帮助救援。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-12 20:32:10

嘿,我已经通过使用HttpServletRequest和JsonSurfer库解开了这个谜团。下面是一个示例代码:

代码语言:javascript
复制
@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();
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65561173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档