2

I have around a 250MB request body that I need to http PUT in Spring 4. I was thinking that the Jackson Streaming API might be a good way to handle this large body since I am getting OOM issues. I only need this to be enabled for a single endpoint. Does anyone know how to set this up for a Spring 4, @RestController? I have seen mention of WebMvcConfigurerAdapter and HttpMessageConverters, but I can't seem to find an example of how to integrate Spring MVC with Jackson Streaming API.

Thx!
-David

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
Ron Stevenson
  • 166
  • 1
  • 13

1 Answers1

2

You can get an InputStream from a request and use it to initialise a JsonParser. It would look like this:

@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) {
         }
    }
}
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82