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 284f8ca
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 562 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);
}
}
7 changes: 2 additions & 5 deletions jaxrs/src/main/java/io/airlift/jaxrs/JaxrsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ 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(JsonMapper.class);
jaxrsBinder(binder).bind(JacksonMapper.class);

if (getProperty("tracing.enabled").map(Boolean::parseBoolean).orElse(false)) {
install(new JaxrsTracingModule());
Expand Down
Loading

0 comments on commit 284f8ca

Please sign in to comment.