Mein MessageBodyWriterResteasy keine benutzerdefinierten Nachrichtentext Schriftsteller erkennen
@Provider
@Produces("text/csv")
public class CSVMessageBodyWriter implements MessageBodyWriter<JaxbList>
public static final String CONTENT_DISPOSITION_HEADER = "Content-Disposition";
//$NON-NLS-1$
private final static HeaderDelegate<ContentDispositionHeader> header = RuntimeDelegate.getInstance().createHeaderDelegate(ContentDispositionHeader.class);
public long getSize(JaxbList t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return CsvSerializer.class.isAssignableFrom(type);
}
public void writeTo(JaxbList t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException, WebApplicationException {
// set content disposition. This will enable browsers to open excel
ContentDispositionHeader contentDispositionHeader = ContentDispositionHeader.createContentDispositionHeader(MediaTypeUtils.CSV_TYPE);
contentDispositionHeader.setFileName("representation"); //$NON-NLS-1$
httpHeaders.putSingle(CONTENT_DISPOSITION_HEADER, header.toString(contentDispositionHeader));
Charset charset = Charset.forName(ProviderUtils.getCharset(mediaType));
OutputStreamWriter writer = new OutputStreamWriter(entityStream, charset);
PrintWriter printWriter = new PrintWriter(writer);
Iterator<String[]> rows = ((CsvSerializer) t).getEntities();
while (rows.hasNext()) {
printWriter.println(CsvWriter.getCSVRow(rows.next()));
}
printWriter.flush();
}
}
Meine REST Anwendung
@Path("app/v3")
@GZIP
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "text/csv"})
public class ApplicationREST
Erweiterung der Anwendungs
@ApplicationPath("/")
public class JaxRsActivator extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> classes = new HashSet<Class<?>>();
public JaxRsActivator() {
singletons.add(new CSVMessageBodyWriter());
classes.add(ApplicationREST.class);
}
@Override
public Set<Object> getSingletons() {
Set<Object> defaults = super.getSingletons();
singletons.addAll(defaults);
return singletons;
}
public Set<Class<?>> getClasses() {
return classes;
}
}
Wenn ich im Debug-Modus laufen kann ich getroffen Meine JaxRsActivator-Klasse, daher weiß ich, dass die Provider geladen werden. Allerdings bekomme ich den Fehler "Konnte MessageBodyWriter für Antwort Objekt vom Typ nicht finden: net.comp.jaxb.JaxbList des Medientyps: text/csv"
Ist 'classes.add (AppREST.class);' ein Tippfehler? Oder hast du gerade vergessen, den ApplicationREST irgendwie verfügbar zu machen? –
das ist ein Tippfehler. sein ApplicationREST –
@Cam Sonaris - Können Sie bitte Ihre MessageBodyWriter-Implementierung posten? – gregwhitaker