I have 20k records in db, I need to export all data into CSV file by implementing StreamingOutput from rest service. I have no idea about, how to implement StreamingOutput for downloading csv file.
Please help me..
Thanks in advance 🙏🙏
You have to read the records from the database, convert each row into CSV and use something like the following to use StreamingOutput
:
package de.demo.services;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.*;
@Path( "/demo" )
public class DemoService
{
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/api")
public Response getCsv() {
StreamingOutput stream = new StreamingOutput() {
public void write(OutputStream os) throws ... {
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
writer.write( /* CSV data */ );
writer.flush();
}
};
return Response.ok(stream).build();
}
}
Be careful not to read all your 20k rows from the database at once but read the results based on a cursor by method setFetchSize
of the JDBC statement.
Use the following dependencies in Maven:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.1</version>
</dependency>
Version 2.23.1
of Jersey is not the latest version you can get.
I am using the same approach, like what u suggested, but control is not going inside the StreamingOutput implementation only..Here is the code....
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/allEmployeeReport")
public Response exportAllEmployee()
{
StreamingOutput stream = new
StreamingOutput() {
public void write(OutputStream os)
throws IOException, Web...{
Writer writer = new
BufferedWriter(new
OutputStreamWriter(os));
for(Employee employee :
repository.findAll()){
writer.write(employee.getFName());
writer.write(",");
writer.write(employee.getLName());
writer.write(",");
writer.write(employee.getEmail());
writer.write(",");
writer.write(employee.getMobile());
writer.write(",");
writer.write(employee.getDOB());
writer.write("\n");
}
writer.flush();
writer.close();
};
return Response.ok(stream).build();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.