使用FastJson作為JAX-RS的序列化/反序列化框架

jopen 9年前發布 | 59K 次閱讀 JSON開發包 fastjson

FastJson是阿里巴巴的溫少開發的一個JSON的序列化和反序列化的框架。 我前面的文章: Java序列化框架性能比較中也提到, 我使用第三方的序列化測試框架jvm-serializers表明, FastJson是最快的JSON序列化框架之一, 優于Jackson。
fastjson.jpg

JAX-RS是一個Java編程語言的應用程序接口,支持按照 表象化狀態轉變 (REST)架構風格創建Web服務Web服務. JAX-RS使用了Java SE 5引入的Java 標注來簡化Web服務客戶端和服務端的開發和部署。
JAX-RS的實現包括:

  • Apache CXF,開源的Web服務框架。
  • Jersey, 由Sun提供的JAX-RS的參考實現。
  • RESTEasy,JBoss的實現。
  • Restlet,由Jerome Louvel和Dave Pawson開發,是最早的REST框架,先于JAX-RS出現。
  • Apache Wink,一個Apache軟件基金會孵化器中的項目,其服務模塊實現JAX-RS規范

如果項目中使用JAX-RS傳遞JSON格式的數據, 可以利用FastJson提高序列化的性能。 但是FastJson并沒有提供JAX-RS的集成( Issue #65, Issue #138 )。

我實現了一個FastJson的JAX-RS集成框架: fastjson-jaxrs-json-provider,可以方便在在JAX-RS項目中使用。

前言

FastJsonProvider is a standard entity provider that follows JAX-RS 2.0 spec.
According to different JAX-RS implementations such as CXF, Jersey, maybe you use FastJsonProvider in appropriate styles.
FastJsonProvider can serialize/deserialize specific types including:

  • all types: public FastJsonProvider() (default constructor)
  • all type annotated with FastJsonType: public FastJsonProvider(boolean annotated)
  • all type annotated in specific packages : public FastJsonProvider(String[] scanpackages)
  • all type annotated in specific packages with FastJsonType: public FastJsonProvider(String[] scanpackages, boolean annotated)
  • all type in specific classes: public FastJsonProvider(Class<?>[] clazzes)

You can init this provider instance with a FastJsonConfig object which is used to configure FastJson features, SerializeConfig, ParserConfig and SerializeFilter. Any parameters can be null and will be default.

Maven

stable version: 0.1.0

<dependency>
<groupId>com.colobu</groupId>
<artifactId>fastjson-jaxrs-json-provider</artifactId>
<version>0.1.0</version>
<dependency>

snapshot version: 0.2.0-0.2.0-SNAPSHOT

<dependency>
<groupId>com.colobu</groupId>
<artifactId>fastjson-jaxrs-json-provider</artifactId>
<version>0.2.0-SNAPSHOT</version>
<dependency>

Jersey configuration

Please check the test and maybe you need to create a FastJsonFeature to override MessageBodyReader and MessageBodyWriter.

CXF confguration

You can use JAXRSServerFactoryBean.setProviders to add a FastJsonProvider instance.


來自:http://colobu.com/2014/09/30/using-fastjson-as-JAX-RS-data-binding/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!