This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Integrate Fastjson in JAXRS
业余布道师 edited this page Sep 12, 2017
·
2 revisions
Fastjson has implemented JAX-RS, You can use FastJson in Restfull Service for data Serialize and Deserialize.
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>
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;
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>
如有需要修改本注脚,请联系阿里巴巴,
© Alibaba Fastjson Develop Team
注明: 版权所有阿里巴巴,请注明版权所有者
If you need to amend this footnote, please contact Alibaba.
© Alibaba Fastjson Develop Team
Note: Copyright Alibaba, please indicate the copyright owner