gpt4 book ai didi

java - Jersey JAXB 如何为 List 实现 MessageBodyWriter

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:30:18 24 4
gpt4 key购买 nike

我正在尝试为 List 实现 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));
}


}

最佳答案

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

关于java - Jersey JAXB 如何为 List 实现 MessageBodyWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8211368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com