How to stream JSON result with Jackson in Vert.x (java) -


I like a Vert.x. like my Java database. It is not very different to write the JSON result as a string in the request stream, as shown below:

  ... routeMatcher.get ("/ myservice / api / get v1 / query /: query ", Req - & gt; {// query string Kwerising = req.params (). (" query "); query = jsonMapper.readValue (querySring, Query.class); // My service makes a list of results as a record ... list & lt; record & gt; results = myservice.query (query); string jsonResult = jsonMapper.writeValueAsString (results); // Level Reqkresponse (). Write the whole title. Header (). Set ( "Content-type", "application / Jason; charset = UTF -8"); Rekkrspanshn (). End (Jesniarslt);}) ; ...  

However, I want to stream object request to Java objects using the method of Jackson have:

  ObjectMapper objectMapper = New ObjectMapper () ; ObjectMapper.writeValue (OutputStream, Results);  

But I do not know how to connect Jackson's outputstream logic to Wert.X's re-argument (), because they have their own system that is Jackson's Java. Io.Outputstream appears to be incompatible with the argument.

Can not I use Jackson in conjunction with Vert.x? Should I write custom serializer with Vertex X. myself? Other suggestions?

I think you are generating large JSON documents because string output for small people is quite Good: objectMapper.writeValue (& lt; string & gt; results);

A problem with the stream ObjectMapper result of size do not know and you will end up with the exception of:

  java.lang.IllegalStateException: the message chattels must set the content-length header to the total size if you are not using encrypted HTTP encoding, any Yangkvrkskjawakkor before sending data. http.impl.DefaultHttpServerResponse.write on ( DefaultHttpServerResponse.java:474)  

In your example, I have a temporary file for JSON output Will use the issues and then respond to them will flush (I have not tested the code)

  file tmpFile = File.createTempFile ( "tmp", ".json"); Mapper.writeValue (tmpFile, results); Req.response () SendFile (tmpFile.getAbsolutePath (), (results) -> tmpFile.delete ());  

If you know the length of the content, then initially you have outputstream with writeStream

  Import org.vertx.java.core.buffer.Buffer; Import org.vertx.java.core.streams.WriteStream; Import java.io.IOException; Import java.io.OutputStream; Public Class OutputWirestream Stream Output Stream {Public WriteStream writeStream; Public runnel closure; @ Override throws the public wide list (int b) IOException {new unsupported operation expansion (throw); } @ Override public Whaid list (byte [] b, Int off, Int len) throws IOException {if (off == 0 & amp; amp; amp; len == b.length) {writeStream.write (new buffer ( B)); Return; } Byte [] bytes = new byte [lane]; System ArrayCP (B, Off, Bytes, 0, Lane); WriteStream.write (new buffer (bytes)); } @ Override Public Wide List (Byte [B] B) IOException throws {writeStream.write (new buffer (b)); } @ Override Public Watch Close () throws IOException {closeHandler.run ()); }}  

Comments