[英]How can I convert a JAX-WS header into a String?
我需要從SOAP消息的標頭中獲取一個值,並且正在使用JAX-WS。 從標題中獲取數據並不容易,這就是我到目前為止所獲得的:
@Resource
private WebServiceContext context;
...
HeaderList headerList = (HeaderList) context.getMessageContext().get(JAXWSProperties.INBOUND_HEADER_LIST_PROPERTY);
Header header = headerList.get(0);
我想將此標頭轉換為其xml表示形式,但是Header API看起來並不容易。 我想我應該說header.readHeader(); 這將返回XMLStreamReader(既不是Stream也不是Reader),並且從那里就像使用類似於Iterator或Enumeration或DOM api的接口一樣。
我可以將此標頭轉換為其xml表示形式的最簡單方法是什么?
public static String prettyPrintXML(XMLStreamReader xmlStreamReader) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
serializer.transform(new StAXSource(xmlStreamReader), new StreamResult(baos));
} catch (Exception e) {
throw new RuntimeException(e);
}
String result = baos.toString();
try {
baos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.