2011-11-21 71 views
6

我正在嘗試爲List實現MessageBodyWriter。在這個實現中,我有一個空行指針異常; marshaller.marshal(o,entityStream);Jersey JAXB如何爲列表實現MessageBodyWriter

@Provider 
@Produces(MediaType.APPLICATION_XML) 
public class MyListProvider implements MessageBodyWriter<List<Instrument>> { 

    private String myWrapElemName = "datas"; 
    private Marshaller marshaller; 


    public InstrumentModelListProvider(){ 

     JAXBContext context; 
     try { 
      context = JAXBContext.newInstance(Data.class); 
      marshaller = context.createMarshaller(); 
      marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); 
      marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); 
      //marshaller.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.TRUE); 
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
     } catch (JAXBException e) { 
      System.err.println("/////////////"+e.getMessage()); 
      //e.printStackTrace(); 
     } 

    } 
    @Override 
    public long getSize(List<Data> as, Class<?> type, Type genericType, Annotation[] annotations, 
      MediaType mediaType) { 

     return -1; 
    } 

    @Override 
    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, 
      MediaType mediaType) { 



     return MediaType.APPLICATION_XML_TYPE.equals(mediaType) 
       && List.class.isAssignableFrom(type) 
       && (((ParameterizedType)genericType).getActualTypeArguments()[0]).equals(Data.class); 
    } 

    @Override 
    public void writeTo(List<Data> list, Class<?> type, Type genericType, Annotation[] annotations, 
      MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, 
      OutputStream entityStream) throws IOException, WebApplicationException { 

     Charset c = Charset.forName("UTF-8"); 
     String cName = c.name(); 

     entityStream.write(String.format("<?xml version=\"1.0\" encoding=\"%s\" standalone=\"yes\"?>", cName).getBytes(cName)); 

     entityStream.write(String.format("<%s>", myWrapElemName).getBytes(cName)); 

     for (Data o : list){ 
      try { 
       marshaller.marshal(o, entityStream); 
      } catch(JAXBException exp) { 
      exp.printStackTrace(); 
       //System.exit(-1); 
      } 
     } 

     entityStream.write(String.format("</%s>", myWrapElemName).getBytes(cName)); 
    } 


} 

回答

2
public void writeTo(List<Data> list, Class<?> type, Type genericType, Annotation[] annotations, 
     MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, 
     OutputStream entityStream) throws IOException, WebApplicationException { 

    Charset c = Charset.forName("UTF-8"); 
    String cName = c.name(); 

    entityStream.write(String.format("<?xml version=\"1.0\" encoding=\"%s\" standalone=\"yes\"?>", cName).getBytes(cName)); 

    entityStream.write(String.format("<%s>", myWrapElemName).getBytes(cName)); 
    InstrumentModelListProvider(); 
    for (Data o : list){ 
     try { 
      marshaller.marshal(o, entityStream); 
     } catch(JAXBException exp) { 
     exp.printStackTrace(); 
      //System.exit(-1); 
     } 
    } 

    entityStream.write(String.format("</%s>", myWrapElemName).getBytes(cName)); 
} 
+0

你是不是編組數據O對象了entityStream?擁有緩衝區的意義是什麼? – Lahniep

+0

我認爲你的錯誤是當(數據)o爲空時,並且你把它放入流中 –

+0

它不能爲null,因爲它來自對Data對象的非空列表的迭代。 – Lahniep

相關問題