Skip to content

Commit

Permalink
Remove custom Json mapper and smile support
Browse files Browse the repository at this point in the history
This reuses Jackson provided Jakarta provider
  • Loading branch information
wendigo committed Nov 28, 2024
1 parent ecbd53e commit 349be74
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 691 deletions.
28 changes: 21 additions & 7 deletions jaxrs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-smile</artifactId>
<groupId>com.fasterxml.jackson.jakarta.rs</groupId>
<artifactId>jackson-jakarta-rs-base</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.jakarta.rs</groupId>
<artifactId>jackson-jakarta-rs-json-provider</artifactId>
</dependency>

<dependency>
Expand All @@ -46,11 +51,6 @@
<artifactId>configuration</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>log</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>units</artifactId>
Expand Down Expand Up @@ -187,4 +187,18 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<ignoredNonTestScopedDependencies>
<!-- incorrectly marked as non-test scoped -->
<ignoredDependency>com.fasterxml.jackson.core:jackson-core</ignoredDependency>
</ignoredNonTestScopedDependencies>
</configuration>
</plugin>
</plugins>
</build>
</project>
165 changes: 0 additions & 165 deletions jaxrs/src/main/java/io/airlift/jaxrs/AbstractJacksonMapper.java

This file was deleted.

37 changes: 37 additions & 0 deletions jaxrs/src/main/java/io/airlift/jaxrs/JacksonMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.airlift.jaxrs;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.core.exc.StreamWriteException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.exc.PropertyBindingException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;

public class JacksonMapper
implements ExceptionMapper<JacksonException>
{
@Override
public Response toResponse(JacksonException exception)
{
return switch (exception) {
// User errors
case StreamReadException streamReadException -> Response.status(Response.Status.BAD_REQUEST)
.entity(formatErrorMessage(streamReadException, "Could not read JSON value")).build();
case PropertyBindingException propertyBindingException -> Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(formatErrorMessage(propertyBindingException, "Could not bind JSON value")).build();
// Server errors
case StreamWriteException streamWriteException -> Response.status(Response.Status.BAD_REQUEST)
.entity(formatErrorMessage(streamWriteException, "Could not write JSON value")).build();
case JsonMappingException mappingException -> Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Could not map JSON value: " + mappingException.getMessage()).build();
default -> Response.serverError().entity(exception.getMessage()).build();
};
}

private static String formatErrorMessage(JsonProcessingException e, String message)
{
return "%s: %s at location %s".formatted(message, e.getOriginalMessage(), e.getLocation());
}
}
11 changes: 0 additions & 11 deletions jaxrs/src/main/java/io/airlift/jaxrs/JaxrsBinder.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package io.airlift.jaxrs;

import com.google.common.collect.ImmutableList;
import com.google.inject.Binder;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.multibindings.Multibinder;
import org.glassfish.jersey.server.wadl.internal.WadlResource;
import org.glassfish.jersey.server.wadl.processor.OptionsMethodProcessor;
import org.glassfish.jersey.server.wadl.processor.WadlModelProcessor;

import java.util.Collection;

import static com.google.inject.Scopes.SINGLETON;
import static com.google.inject.multibindings.Multibinder.newSetBinder;
Expand Down Expand Up @@ -53,9 +47,4 @@ public void bindInstance(Object instance)
{
resourceBinder.addBinding().toInstance(instance);
}

public static Collection<Class<?>> getBuiltinResources()
{
return ImmutableList.of(WadlResource.class, WadlModelProcessor.class, OptionsMethodProcessor.class);
}
}
22 changes: 17 additions & 5 deletions jaxrs/src/main/java/io/airlift/jaxrs/JaxrsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/
package io.airlift.jaxrs;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jakarta.rs.cfg.JakartaRSFeature;
import com.fasterxml.jackson.jakarta.rs.json.JacksonJsonProvider;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Binder;
import com.google.inject.Inject;
import com.google.inject.Key;
import com.google.inject.Provides;
import com.google.inject.multibindings.ProvidesIntoSet;
import io.airlift.configuration.AbstractConfigurationAwareModule;
import io.airlift.jaxrs.tracing.JaxrsTracingModule;
import jakarta.servlet.Servlet;
Expand All @@ -27,6 +32,7 @@

import java.util.Set;

import static com.fasterxml.jackson.core.JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.inject.multibindings.Multibinder.newSetBinder;
import static io.airlift.jaxrs.JaxrsBinder.jaxrsBinder;
Expand All @@ -47,13 +53,9 @@ public JaxrsModule(boolean requireExplicitBindings)
protected void setup(Binder binder)
{
binder.disableCircularProxies();

binder.bind(Servlet.class).to(Key.get(ServletContainer.class));
jaxrsBinder(binder).bind(JsonMapper.class);
jaxrsBinder(binder).bind(SmileMapper.class);
jaxrsBinder(binder).bind(ParsingExceptionMapper.class);

newSetBinder(binder, Object.class, JaxrsResource.class).permitDuplicates();
jaxrsBinder(binder).bind(JacksonMapper.class);

if (getProperty("tracing.enabled").map(Boolean::parseBoolean).orElse(false)) {
install(new JaxrsTracingModule());
Expand All @@ -72,4 +74,14 @@ public static ResourceConfig createResourceConfig(@JaxrsResource Set<Object> jax
return new JaxrsResourceConfig(jaxRsSingletons)
.setProperties(ImmutableMap.of(RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true"));
}

@ProvidesIntoSet
@Inject
@JaxrsResource
public static JacksonJsonProvider jsonProvider(ObjectMapper objectMapper)
{
return new JacksonJsonProvider(objectMapper)
.enable(JakartaRSFeature.ADD_NO_SNIFF_HEADER)
.enable(INCLUDE_SOURCE_IN_LOCATION);
}
}
Loading

0 comments on commit 349be74

Please sign in to comment.