Skip to content

Commit

Permalink
jax-rs support (#3110)
Browse files Browse the repository at this point in the history
* jax-rs support

* fix error: reference not found
  • Loading branch information
rowstop authored Oct 21, 2024
1 parent 15a8d12 commit 6a2ca63
Show file tree
Hide file tree
Showing 24 changed files with 1,146 additions and 0 deletions.
55 changes: 55 additions & 0 deletions extension-jaxrs/extension-jaxrs-jakarta/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-extension-jaxrs</artifactId>
<version>2.0.54-SNAPSHOT</version>
</parent>

<artifactId>fastjson2-extension-jaxrs-jakarta</artifactId>

<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>

<jersey.version>3.1.0</jersey.version>
</properties>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${jersey.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>${jakarta.ws.rs.version}</version>
<scope>provided</scope>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jdk-http</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.alibaba.fastjson2.support.jaxrs.jakarta;

import jakarta.annotation.Priority;
import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.core.FeatureContext;
import org.glassfish.jersey.internal.spi.AutoDiscoverable;

/**
* FastJson2AutoDiscoverable
* 参考: com.alibaba.fastjson.support.jaxrs.FastJsonAutoDiscoverable
*
* @author 张治保
* @since 2024/10/16
* @see AutoDiscoverable
*/
@Priority(AutoDiscoverable.DEFAULT_PRIORITY - 1)
public class FastJson2AutoDiscoverable
implements AutoDiscoverable {
public static final String FASTJSON_AUTO_DISCOVERABLE = "fastjson.auto.discoverable";
public static volatile boolean autoDiscover;

static {
autoDiscover = Boolean.parseBoolean(
System.getProperty(FASTJSON_AUTO_DISCOVERABLE, Boolean.TRUE.toString()));
}

@Override
public void configure(final FeatureContext context) {
final Configuration config = context.getConfiguration();

// Register FastJson.
if (!config.isRegistered(FastJson2Feature.class) && autoDiscover) {
context.register(FastJson2Feature.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.alibaba.fastjson2.support.jaxrs.jakarta;

import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.core.Feature;
import jakarta.ws.rs.core.FeatureContext;
import jakarta.ws.rs.ext.MessageBodyReader;
import jakarta.ws.rs.ext.MessageBodyWriter;
import org.glassfish.jersey.CommonProperties;
import org.glassfish.jersey.internal.InternalProperties;
import org.glassfish.jersey.internal.util.PropertiesHelper;
/**
* FastJson2Feature
* 参考:com.alibaba.fastjson.support.jaxrs.FastJsonFeature
*
* @author 张治保
* @since 2024/10/16
* @see Feature
*/
public class FastJson2Feature
implements Feature {
private static final String JSON_FEATURE = FastJson2Feature.class.getSimpleName();

@Override
public boolean configure(final FeatureContext context) {
try {
final Configuration config = context.getConfiguration();

final String jsonFeature = CommonProperties.getValue(
config.getProperties(),
config.getRuntimeType(),
InternalProperties.JSON_FEATURE,
JSON_FEATURE,
String.class
);

// Other JSON providers registered.
if (!JSON_FEATURE.equalsIgnoreCase(jsonFeature)) {
return false;
}

// Disable other JSON providers.
context.property(
PropertiesHelper.getPropertyNameForRuntime(
InternalProperties.JSON_FEATURE,
config.getRuntimeType()
),
JSON_FEATURE
);

// Register FastJson.
if (!config.isRegistered(FastJson2Provider.class)) {
context.register(FastJson2Provider.class, MessageBodyReader.class, MessageBodyWriter.class);
}
} catch (NoSuchMethodError e) {
// skip
}

return true;
}
}
Loading

0 comments on commit 6a2ca63

Please sign in to comment.