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 Aug 11, 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

Clone this wiki locally