我的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媒体类型:text / csv”
答案 0 :(得分:5)
根据您的上述代码,您的CSVMessageBodyWriter
会被调用,但isWriteable
检查失败。
您的isWriteable
支票总是会返回false。您正在检查JaxbList
是否可以CSVSerializer
分配CSVMessageBodyWriter
。这总是会失败,您的text/csv
将被视为无法处理public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
{
return true;
}
。
尝试将isWriteable方法更改为以下内容:
text/csv
这将序列化所有JaxbList
带注释的方法。如果您想将其约束为public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
{
ParameterizedType paramType = (ParameterizedType) genericType;
if(paramType.getRawType().equals(JaxbList.class))
{
return true;
}
return false;
}
,那么您可以执行以下操作:
MessageBodyWriter
以下是配置自定义public class ProductGuideApplication extends Application
{
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> classes = new HashSet<Class<?>>();
public ProductGuideApplication()
{
singletons.add(new CSVMessageBodyWriter());
classes.add(FooResource.class);
}
@Override
public Set<Object> getSingletons()
{
return singletons;
}
@Override
public Set<Class<?>> getClasses()
{
return classes;
}
}
的简单工作示例:
<强>应用强>
@Path("/foo")
public class FooResource
{
@GET
@Produces("text/csv")
public List<Consumer> getConsumers()
{
Consumer consumer1 = new Consumer();
consumer1.setId("1234");
consumer1.setGender("Male");
Consumer consumer2 = new Consumer();
consumer2.setId("2345");
consumer2.setGender("Male");
List<Consumer> consumers = new ArrayList<Consumer>();
consumers.add(consumer1);
consumers.add(consumer2);
return consumers;
}
}
<强>资源强>
@Provider
@Produces("text/csv")
public class CSVMessageBodyWriter implements MessageBodyWriter<List<Consumer>>
{
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
{
ParameterizedType paramType = (ParameterizedType) genericType;
if(paramType.getRawType().equals(List.class))
{
if(paramType.getActualTypeArguments()[0].equals(Consumer.class))
{
return true;
}
}
return false;
}
@Override
public long getSize(List<Consumer> t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
{
return 0;
}
@Override
public void writeTo(List<Consumer> t, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException,
WebApplicationException
{
//Write your CSV to entityStream here.
}
}
<强> MessageBodyWriter 强>
{{1}}