【发布时间】:2013-07-11 12:31:06
【问题描述】:
我的 MessageBodyWriter
@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();
}
}
我的 REST 应用程序
@Path("app/v3")
@GZIP
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "text/csv"})
public class ApplicationREST
应用扩展
@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;
}
}
当我在调试模式下运行时,我可以点击我的 JaxRsActivator 类,所以我知道提供程序正在加载。但是我收到错误“找不到类型的响应对象的 MessageBodyWriter:net.comp.jaxb.JaxbList of media type: text/csv”
【问题讨论】:
-
classes.add(AppREST.class);是错字吗?还是您只是忘记以某种方式使 ApplicationREST 可用? -
这是一个错字。它的ApplicationREST
-
@Cam Sonaris - 你能发布你的 MessageBodyWriter 实现吗?
-
@GregWhitaker 进行了编辑,但是我不确定这有什么帮助,因为它甚至没有影响到课堂
-
@Cam Sonaris - 您是否尝试将类添加到 web.xml 中的 resteasy.providers 上下文参数?