2011-12-07 21 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'。我正在把正確的絕對路徑。 –