Jersey JAXB 如何实现 List 的 MessageBodyWriter

发布于 2024-12-16 23:32:10 字数 2511 浏览 0 评论 0原文

我正在尝试为列表实现 MessageBodyWriter。通过这个实现,我在线出现空指针异常; marshaller.marshal(o,entityStream);

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


}

I am trying to implement a MessageBodyWriter for List. with this implementation i have a null pointer exception on line ; marshaller.marshal(o, entityStream);

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


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花ソ最美的依靠 2024-12-23 23:32:10
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));
}
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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文