Java RESTEasy不';无法识别自定义邮件正文编写器

Java RESTEasy不';无法识别自定义邮件正文编写器,java,jax-rs,resteasy,Java,Jax Rs,Resteasy,我的短信写手 @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 H

我的短信写手

@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();
    }
}
延长适用期

@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;
  }
}
@ApplicationPath(“/”)
公共类JAXRActivator扩展了应用程序{
private Set singleton=new HashSet();
私有集>();
公共JAXSActivator(){
添加(新的CSVMessageBodyWriter());
添加(ApplicationREST.class);
}
@凌驾
公共集getSingleton(){
设置默认值=super.getSingleton();
singletons.addAll(默认值);
返回单身人士;
}

公共集根据上面的代码,似乎正在调用
CSVMessageBodyWriter
,但它未通过
isWriteable
检查

您的
isWriteable
检查将始终返回false。您正在检查
JaxbList
是否可从
CSVSerializer
分配。这将始终失败,并且您的
CSVMessageBodyWriter
将被视为无法处理
文本/csv

尝试将isWriteable方法更改为以下内容:

public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) 
{
    return true;
}

下面是一个配置自定义
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;
    }
}
公共类ProductGuideApplication扩展应用程序
{
private Set singleton=new HashSet();
私有集>();
公共产品指南应用程序()
{ 
添加(新的CSVMessageBodyWriter());
添加(FooResource.class);
}
@凌驾
公共集getSingleton()
{ 
返回单身人士;
}
@凌驾

公共集合
classes.add(AppREST.class)
一个输入错误?还是你只是忘记了以某种方式使ApplicationREST可用?那是一个输入错误。它是ApplicationREST@CamSonaris-你能发布你的MessageBodyWriter实现吗?@GregWhitaker做了编辑,但是我不确定这有什么帮助,因为它甚至没有击中目标class@CamSonaris-您是否尝试将该类添加到web.xml中的resteasy.providers上下文参数?
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.
    }
}