Java CXF提供程序服务(jax ws)
我需要解析一个巨大的xml请求,因此我使用cxf提供程序,但是使用这个提供程序类,我必须返回与输入类对象相同的类对象 我的问题是,在处理输入StAXSource时,如何从提供程序返回带有修改的xml流的输出StAXSource 我的代码Java CXF提供程序服务(jax ws),java,soap,cxf,Java,Soap,Cxf,我需要解析一个巨大的xml请求,因此我使用cxf提供程序,但是使用这个提供程序类,我必须返回与输入类对象相同的类对象 我的问题是,在处理输入StAXSource时,如何从提供程序返回带有修改的xml流的输出StAXSource 我的代码 @WebServiceProvider( portName = "MyService", serviceName = "service", targetNamespace = "m
@WebServiceProvider(
portName = "MyService", serviceName = "service",
targetNamespace = "mytarget.space.com",
wsdlLocation = "WEB-INF/wsdl/my/wsd-file.wsdl")
public class MyServiceProvider implements Provider<StAXSource> {
@Override
public StAXSource invoke(StAXSource stAXSource) {
XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
XMLEventReader xmlEventReader;
XMLEventWriter xmlEventWriter;
StAXSource outpuStaxSource = null;
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(16 * 1024);
xmlEventReader = xmlInputFactory.createXMLEventReader(stAXSource.getXMLStreamReader());
xmlEventWriter = xmlOutputFactory.createXMLEventWriter(byteArrayOutputStream);
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new InputStreamReader(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
outpuStaxSource = new StAXSource(xmlStreamReader);
while (xmlEventReader.hasNext()){
xmlEventWriter.add(xmlEventReader.nextEvent());
xmlEventWriter.flush();
xmlStreamReader.next();
}
} catch (Throwable er){
}
return outpuStaxSource;
}
}
更新
因此,基本上我正在寻找一种直接将数据返回到流的方法,如本文()所述,但我需要对CXF和JAX-WS执行相同的操作,而不是rest
链接摘录
StreamingResponseBody stream = out -> {
final String home = System.getProperty("user.home");
final File directory = new File(home + File.separator + "Documents" + File.separator + "sample");
final ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
if(directory.exists() && directory.isDirectory()) {
try {
for (final File file : directory.listFiles()) {
final InputStream inputStream=new FileInputStream(file);
final ZipEntry zipEntry=new ZipEntry(file.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes=new byte[1024];
int length;
while ((length=inputStream.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
inputStream.close();
}
zipOut.close();
} catch (final IOException e) {
logger.error("Exception while reading and streaming data {} ", e);
}
StreamingResponseBody stream = out -> {
final String home = System.getProperty("user.home");
final File directory = new File(home + File.separator + "Documents" + File.separator + "sample");
final ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
if(directory.exists() && directory.isDirectory()) {
try {
for (final File file : directory.listFiles()) {
final InputStream inputStream=new FileInputStream(file);
final ZipEntry zipEntry=new ZipEntry(file.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes=new byte[1024];
int length;
while ((length=inputStream.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
inputStream.close();
}
zipOut.close();
} catch (final IOException e) {
logger.error("Exception while reading and streaming data {} ", e);
}