Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Integrate Fastjson in JAXRS

业余布道师 edited this page Sep 12, 2017 · 2 revisions

Integrate Fastjson in JAX-RS

Fastjson has implemented JAX-RS, You can use FastJson in Restfull Service for data Serialize and Deserialize.

Use Fastjson in Apache CXF

With Apache CXF Restful and Spring Framework, for example

<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
    <!-- ... -->
</bean>

<bean id="fastJsonProvider" class="com.alibaba.fastjson.support.jaxrs.FastJsonProvider">
    <property name="fastJsonConfig" ref="fastJsonConfig"/>
</bean>

<jaxrs:server id="restfulServer" address="/">
    <jaxrs:inInterceptors>
        <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
    </jaxrs:inInterceptors>
    <jaxrs:outInterceptors>
        <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
    </jaxrs:outInterceptors>
    <jaxrs:serviceBeans>
        <ref bean="Your service" />
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <ref bean="fastJsonProvider" /><!-- FastJsonProvider -->
    </jaxrs:providers>
    <jaxrs:extensionMappings>
        <entry key="json" value="application/json" />
    </jaxrs:extensionMappings>
    <jaxrs:languageMappings>
        <entry key="en" value="en-gb" />
    </jaxrs:languageMappings>
</jaxrs:server>

REF: Apache CXF Restful official document

Use Fastjson in Jersey

User register codes, since version 1.2.37 (recommend)

@Provider
static class FastJsonResolver implements ContextResolver<FastJsonConfig> {

    public FastJsonConfig getContext(Class<?> type) {

        FastJsonConfig fastJsonConfig = new FastJsonConfig();

        /* ... */

        return fastJsonConfig;
    }
}

public class RestApplication extends ResourceConfig { 
 
    public RestApplication() {     

     packages("com.xxx.xxx");  

     /* ... */

     register(FastJsonResolver.class);     

     register(FastJsonFeature.class);  
    }  
}  

Note: Fastjson also provided auto register when you don't have to user register in Jersey, default is enabled, if you don't want this, you can

FastJsonAutoDiscoverable.autoDiscover = false;

REF: Jersey deployment official document

Use Fastjson in RestEasy

Enable RestEasy auto scan, like this in your pom.xml:

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-servlet-initializer</artifactId>
    <version>${resteasy.version}</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>${fastjson.version}</version>
</dependency>

<!-- must exclude other json
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson-provider</artifactId>
    <version>${resteasy.version}</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson2-provider</artifactId>
    <version>${resteasy.version}</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxb-provider</artifactId>
    <version>${resteasy.version}</version>
</dependency>
-->

or Disable auto scan, like this in your web.xml:

<context-param>
    <param-name>resteasy.scan.providers</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>resteasy.providers</param-name> 
    <param-value>com.alibaba.fastjson.support.jaxrs.FastJsonProvider</param-value>
</context-param>

REF: RestEasy configuration official document

Clone this wiki locally