2013-07-11 38 views
5

MessageBodyWriter MyRESTEasy không nhận ra tùy chỉnh văn nội dung thư

@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(); 
    } 
} 

My REST của Application

@Path("app/v3") 
@GZIP 
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "text/csv"}) 
public class ApplicationREST 

Gia hạn Application

@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; 
    } 
} 

Khi tôi chạy trong chế độ gỡ lỗi tôi có thể nhấn lớp JaxRsActivator của tôi, vì vậy tôi biết rằng các nhà cung cấp đang được tải. Tuy nhiên tôi nhận được lỗi "Không thể tìm thấy MessageBodyWriter cho đối tượng phản hồi của loại: net.comp.jaxb.JaxbList loại phương tiện: text/csv"

+0

là 'classes.add (AppREST.class); 'một lỗi đánh máy? Hay bạn chỉ quên bằng cách nào đó làm cho ApplicationREST có sẵn? –

+0

đó là lỗi đánh máy. Ứng dụng của nóREST –

+0

@Cam Sonaris - Bạn có thể vui lòng đăng triển khai MessageBodyWriter của mình không? – gregwhitaker

Trả lời

5

Dựa trên mã của bạn ở trên có vẻ như bạn đang gọi CSVMessageBodyWriter nhưng không thực hiện được séc isWriteable của bạn.

Séc isWriteable của bạn luôn trả về false. Bạn đang kiểm tra xem JaxbList có thể được chuyển nhượng từ CSVSerializer hay không. Điều này luôn luôn thất bại và CSVMessageBodyWriter của bạn sẽ không được coi là có thể xử lý text/csv.

Hãy thử thay đổi phương pháp isWriteable của bạn như sau:

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

này sẽ serialize tất cả text/csv phương pháp được chú thích. Nếu bạn muốn để hạn chế nó để JaxbList sau đó bạn có thể làm một cái gì đó như thế này:

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; 
} 

Dưới đây là một ví dụ làm việc đơn giản của cấu hình tùy chỉnh MessageBodyWriter:

Application

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; 
    } 
} 

Tài nguyên

@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; 
    } 
} 

MessageBodyWriter

@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. 
    } 
} 
+0

Cảm ơn bạn rất nhiều! –

Các vấn đề liên quan