[DEV] add csv serializator for user export

This commit is contained in:
Edouard DUPIN 2024-02-03 18:46:30 +01:00
parent eca28292d5
commit 29402fc27e
2 changed files with 63 additions and 1 deletions

View File

@ -107,7 +107,12 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.0</version>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>

View File

@ -0,0 +1,57 @@
package org.kar.archidata.serializer;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyWriter;
import jakarta.ws.rs.ext.Provider;
/**
* Body writter use in jersey with :
* In your main:
* ```java
* rc.register(new CSVMessageBodyWritter());
* ```
*
* and in the produce element:
* ```java
* @GET
* @Produces("text/csv")
* public List<Data> getData() {}
* ```
*/
@Provider
@Produces("text/csv")
public class CSVMessageBodyWritter implements MessageBodyWriter<List<?>> {
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return List.class.isAssignableFrom(type);
}
@Override
public long getSize(List<?> data, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return 0;
}
@Override
public void writeTo(List<?> data, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
if (data != null && data.size() > 0) {
CsvMapper mapper = new CsvMapper();
Object o = data.get(0);
CsvSchema schema = mapper.schemaFor(o.getClass()).withHeader();
mapper.writer(schema).writeValue(entityStream, data);
}
}
}