Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StreamingOutput equivalent in Spring MVC

Tags:

java

spring

I have created a web application in spring boot in which I want to use Spring MVC rest framework instead of jersey. I am trying to do something like this but it gives me error. I want a equivalent of StreamingOutput in Spring MVC.

Ex: This uses JAX-RS StreamingOutput

public static StreamingOutput buildErrorEntity(Object error)
{
    StreamingOutput stream = outputStream -> {
        PrintStream printStream = new PrintStream(outputStream);
        new ObjectMapper().writeValue(printStream, error);
    };
    return stream;
}

I want to replace SteamingOutput but it gives me an error saying "Cannot resolve constructor PrintStream()

public static OutputStream buildErrorEntity1(Object error) {

    OutputStream stream = outputStream -> {
        PrintStream printStream = new PrintStream(outputStream);
        new ObjectMapper().writeValue(printStream, error);
    };

    return stream;
}

I would really appreciate some help. Thank you.

like image 408
user1563348 Avatar asked Feb 26 '15 16:02

user1563348


People also ask

When to use StreamingResponseBody?

StreamingResponseBody is used for asynchronous request processing where the application can write directly to the response OutputStream.

What is StreamingResponseBody?

Interface StreamingResponseBody A controller method return value type for asynchronous request processing where the application can write directly to the response OutputStream without holding up the Servlet container thread.

How do you use ResponseBodyEmitter?

GET) public ResponseBodyEmitter handle() { ResponseBodyEmitter emitter = new ResponseBodyEmitter(); // Pass the emitter to another component... return emitter; } // in another thread emitter. send(foo1); // and again emitter. send(foo2); // and done emitter.


1 Answers

They've added StreamingResponseBody in Spring 4.2.

like image 50
Dmitry Zvorygin Avatar answered Oct 10 '22 22:10

Dmitry Zvorygin