如何使用 Spring 4 MVC 配置 Jackson Streaming API

how to configure the Jackson Streaming API with Spring 4 MVC

我有一个大约 250MB 的请求正文,我需要在 Spring 中使用 http PUT 4。我认为 Jackson Streaming API 可能是处理这个大正文的好方法,因为我我遇到了 OOM 问题。我只需要为单个端点启用它。有谁知道如何设置 Spring 4,@RestController?我看到提到 WebMvcConfigurerAdapterHttpMessageConverters,但我似乎找不到如何将 Spring MVC 与 Jackson Streaming API.[=15= 集成的示例]

谢谢!
-大卫

您可以从请求中获取 InputStream 并使用它来初始化 JsonParser。它看起来像这样:

@RestController
public class MyController {

    private static final JsonFactory jfactory = new JsonFactory();

    @PostMapping(path = "/bigfileshere")
    public void enpointForBigFiles(HttpServletRequest request, HttpServletResponse response) {
         InputStream stream = request.getInputStream();
         try (JsonParser parser = jfactory.createParser(stream)) {
             while (parser.nextToken() != JsonToken.END_OBJECT) {
                 String fieldname = parser.getCurrentName();
                 // do other stuff
             }
         } catch (IOException e) {
         }
    }
}