RESTEasy无法识别自定义邮件正文编写器
我的MessageBodyWriter
@Provider @Produces("text/csv") public class CSVMessageBodyWriter implements MessageBodyWriter public static final String CONTENT_DISPOSITION_HEADER = "Content-Disposition"; //$NON-NLS-1$ private final static HeaderDelegate 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 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 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 singletons = new HashSet(); private Set<Class> classes = new HashSet<Class>(); public JaxRsActivator() { singletons.add(new CSVMessageBodyWriter()); classes.add(ApplicationREST.class); } @Override public Set getSingletons() { Set defaults = super.getSingletons(); singletons.addAll(defaults); return singletons; } public Set<Class> getClasses() { return classes; } }
当我在调试模式下运行时,我可以点击我的JaxRsActivator类,所以我知道正在加载提供程序。 但是我收到错误“无法找到类型的响应对象的MessageBodyWriter:net.comp.jaxb.JaxbList媒体类型:text / csv”
根据上面的代码,看起来你的CSVMessageBodyWriter
被调用了,但是你的isWriteable
检查失败了。
您的isWriteable
检查总是返回false。 您正在检查是否可以从CSVSerializer
分配CSVSerializer
。 这总是会失败,您的CSVMessageBodyWriter
将被视为无法处理text/csv
。
尝试将isWriteable方法更改为以下内容:
public boolean isWriteable(Class> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return true; }
这将序列化所有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
资源
@Path("/foo") public class FooResource { @GET @Produces("text/csv") public List getConsumers() { Consumer consumer1 = new Consumer(); consumer1.setId("1234"); consumer1.setGender("Male"); Consumer consumer2 = new Consumer(); consumer2.setId("2345"); consumer2.setGender("Male"); List consumers = new ArrayList (); consumers.add(consumer1); consumers.add(consumer2); return consumers; } }
MessageBodyWriter
@Provider @Produces("text/csv") public class CSVMessageBodyWriter implements MessageBodyWriter> { @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 t, Class> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return 0; } @Override public void writeTo(List t, Class> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { //Write your CSV to entityStream here. } }