日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jaxb注解使用_使用JAXB时

發布時間:2023/12/3 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jaxb注解使用_使用JAXB时 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

jaxb注解使用

并沒有很多例子可以說明這一點,但是如何在應用程序中使用JAXB可以在性能(和內存使用)方面產生巨大差異。

這個例子

在此博客文章中,我將使用一個名為Membership的示例對象,看起來像這樣:

我們將使用JAXB將對象與XML之間的封送進行封送處理。

在靜態塊中創建上下文(或至少一次)

我通常看到的最大錯誤是,每個請求都會創建JAXB上下文:

public String marshal(Membership membership){ StringWriter stringWriter = new StringWriter(); try { JAXBContext context = JAXBContext.newInstance(Membership. class ); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(membership, stringWriter); String xml = stringWriter.toString(); stringWriter.close(); return xml; } catch (JAXBException | IOException ex) { throw new RuntimeException(ex); } } public Membership unmarshal(String xml) { try { JAXBContext context = JAXBContext.newInstance(Membership. class ); Unmarshaller u = context.createUnmarshaller(); return (Membership)u.unmarshal( new StringReader(xml)); } catch (JAXBException ex) { throw new RuntimeException(ex); } }

(也可參見示例代碼這里 )

這里的問題是創建上下文的JAXBContext.newInstance方法。 上下文僅在對象結構更改時更改,并且僅在代碼更改時發生,因此我們可以安全地只執行一次,因此將其更改為在靜態塊中創建,如下所示:

public String marshal(Membership memberships){ StringWriter stringWriter = new StringWriter(); try { Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(memberships, stringWriter); String xml = stringWriter.toString(); stringWriter.close(); return xml; } catch (JAXBException | IOException ex) { throw new RuntimeException(ex); } } public Membership unmarshal(String xml) { try { Unmarshaller u = context.createUnmarshaller(); return (Membership)u.unmarshal( new StringReader(xml)); } catch (JAXBException ex) { throw new RuntimeException(ex); } } private static JAXBContext context; static { try { context = JAXBContext.newInstance(Membership. class ); } catch (JAXBException ex) { throw new RuntimeException(ex); } }

(也可參見示例代碼這里 )

因此,讓我們看看有什么不同。

批處理示例。

如果我們在一個循環中(一次一個)將10000個對象與XML相互轉換,則結果如下:

with Bad util Testing 10000 Marshal took: 10804 ms 13762 Unmarshal took: 13762 ms

然后加上靜態塊:

with Good util Testing 10000 Marshal took: 90 ms Unmarshal took: 428 ms

那就是將編組速度提高120倍,將編組速度提高32倍!!

( 這里有完整的示例)

并發示例。

同樣,當對多個并發請求執行此操作時,您應該看到相同的結果。 因此,當我們將其部署到某個服務器上(在我的示例中是扎帶 ),并將REST端點暴露給封送和封送時,我們可以使用諸如siege之類的東西來生成并發流量到服務器:

錯誤示例的輸出:

Transactions: 255 hits Availability: 100.00 % Elapsed time: 7.91 secs Data transferred: 0.54 MB Response time: 5.13 secs Transaction rate: 32.24 trans/sec Throughput: 0.07 MB/sec Concurrency: 165.52 Successful transactions: 255 Failed transactions: 0 Longest transaction: 6.88 Shortest transaction: 3.47

好例子的輸出:

Transactions: 255 hits Availability: 100.00 % Elapsed time: 1.80 secs Data transferred: 0.53 MB Response time: 0.52 secs Transaction rate: 141.67 trans/sec Throughput: 0.30 MB/sec Concurrency: 73.12 Successful transactions: 255 Failed transactions: 0 Longest transaction: 0.78 Shortest transaction: 0.05

注意“并發性”值的差異(并發性是平均同時連接數,該數字隨著服務器性能下降而增加)

( 這里有完整的示例)

當文件很大時。

如果輸入文件太大,則可能會收到java.lang.OutOfMemoryError異常。

為了確保可以有效地處理大文件,可以確保在創建輸入時正在使用SAX Parser:

public Membership unmarshalWithSAX(InputStream xml){ try { InputSource inputSource = new InputSource(xml); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware( true ); spf.setValidating( true ); SAXParser saxParser = spf.newSAXParser(); saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); XMLReader xmlReader = saxParser.getXMLReader(); SAXSource source = new SAXSource(xmlReader, inputSource); Unmarshaller u = context.createUnmarshaller(); return (Membership)u.unmarshal(source); } catch (ParserConfigurationException | SAXException | JAXBException ex) { throw new RuntimeException(ex); } } private static final String JAXP_SCHEMA_LANGUAGE = " http://java.sun.com/xml/jaxp/properties/schemaLanguage " ; private static final String W3C_XML_SCHEMA = " http://www.w3.org/2001/XMLSchema " ;

( 這里有完整的示例)

全部得到

您可以在一個簡單的庫中獲得所有“好”的東西:

在代碼中使用它

(參見https://github.com/phillip-kruger/jaxb-lib )

<dependency> <groupId>com.github.phillip-kruger.jaxb-library</groupId> <artifactId>jaxb-lib</artifactId> <version> 1.0 . 0 </version> </dependency>

元帥

JaxbUtil jaxbUtil = new JaxbUtil(); byte [] xml = jaxbUtil.marshal(myJAXBObject);

元帥

JaxbUtil jaxbUtil = new JaxbUtil(); MyJAXBObject myJAXBObject = jaxbUtil.unmarshal(MyJAXBObject. class ,xml);

獲取JAXB對象的XSD

XsdUtil xsdUtil = new XsdUtil(); String xsd = xsdUtil.getXsd(MyJAXBObject. class );

翻譯自: https://www.javacodegeeks.com/2019/05/using-jaxb.html

jaxb注解使用

總結

以上是生活随笔為你收集整理的jaxb注解使用_使用JAXB时的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。