2011-12-07 22 views

回答

0

您可以从JAX-RS服务返回javax.ws.rs.core.StreamingOutput。在您实现StreamingOutput时,您可以将JAXB与XSLT和XSD配合使用。

public class MyStreamingOutput implements StreamingOutput { 

    private JAXBContext jc; 
    private Transformer t; 
    private XmlSchema s; 
    private Object o; 

    public MyStreamingOutput(JAXBContext jc, Transformer t, XmlSchema s, Object o) { 
     this.jc = jc; 
     this.t = t; 
     this.s = s; 
     this.o = o; 
    } 

    public void write(java.io.OutputStream output) throws java.io.IOException, WebApplicationException 
     Marshaller m = jc.createMarshaller(); 
     m.setSchema(s); 
     JAXBSource source = new JAXBSource(m,o): 
     StreamResult result = new StreamResult(output); 
     t.transform(source, result); 
    } 
} 
+0

它似乎工作,但是当我尝试指定源作为新的流源时,即使我指定文件的绝对路径,我也会收到systemID错误。不知道什么是错的。 –

+0

你的意思是'StreamResult'?是的,你可能需要做setSystemId(String)。奇怪你仍然看到一个例外。 –

+1

'分配't'时'TransformerFactory.newInstance()。newTransformer(新的StreamsSource(String SystemId))''。那就是我得到未知的'SystemId'。我正在把正确的绝对路径。 –