Skip to content

Commit 0e9b459

Browse files
committed
Remove custom Json mapper and smile support
This reuses Jackson provided Jakarta provider
1 parent e89358c commit 0e9b459

11 files changed

+118
-555
lines changed

jaxrs/pom.xml

+21-7
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@
2828
</dependency>
2929

3030
<dependency>
31-
<groupId>com.fasterxml.jackson.dataformat</groupId>
32-
<artifactId>jackson-dataformat-smile</artifactId>
31+
<groupId>com.fasterxml.jackson.jakarta.rs</groupId>
32+
<artifactId>jackson-jakarta-rs-base</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>com.fasterxml.jackson.jakarta.rs</groupId>
37+
<artifactId>jackson-jakarta-rs-json-provider</artifactId>
3338
</dependency>
3439

3540
<dependency>
@@ -46,11 +51,6 @@
4651
<artifactId>configuration</artifactId>
4752
</dependency>
4853

49-
<dependency>
50-
<groupId>io.airlift</groupId>
51-
<artifactId>log</artifactId>
52-
</dependency>
53-
5454
<dependency>
5555
<groupId>io.airlift</groupId>
5656
<artifactId>units</artifactId>
@@ -182,4 +182,18 @@
182182
<scope>test</scope>
183183
</dependency>
184184
</dependencies>
185+
<build>
186+
<plugins>
187+
<plugin>
188+
<groupId>org.apache.maven.plugins</groupId>
189+
<artifactId>maven-dependency-plugin</artifactId>
190+
<configuration>
191+
<ignoredNonTestScopedDependencies>
192+
<!-- incorrectly marked as non-test scoped -->
193+
<ignoredDependency>com.fasterxml.jackson.core:jackson-core</ignoredDependency>
194+
</ignoredNonTestScopedDependencies>
195+
</configuration>
196+
</plugin>
197+
</plugins>
198+
</build>
185199
</project>

jaxrs/src/main/java/io/airlift/jaxrs/AbstractJacksonMapper.java

-165
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.airlift.jaxrs;
2+
3+
import com.fasterxml.jackson.core.JacksonException;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.core.exc.StreamReadException;
6+
import com.fasterxml.jackson.core.exc.StreamWriteException;
7+
import com.fasterxml.jackson.databind.JsonMappingException;
8+
import com.fasterxml.jackson.databind.exc.PropertyBindingException;
9+
import jakarta.ws.rs.core.Response;
10+
import jakarta.ws.rs.ext.ExceptionMapper;
11+
12+
public class JacksonMapper
13+
implements ExceptionMapper<JacksonException>
14+
{
15+
@Override
16+
public Response toResponse(JacksonException exception)
17+
{
18+
return switch (exception) {
19+
// User errors
20+
case StreamReadException streamReadException -> Response.status(Response.Status.BAD_REQUEST)
21+
.entity(formatErrorMessage(streamReadException, "Could not read JSON value")).build();
22+
case PropertyBindingException propertyBindingException -> Response.status(Response.Status.INTERNAL_SERVER_ERROR)
23+
.entity(formatErrorMessage(propertyBindingException, "Could not bind JSON value")).build();
24+
// Server errors
25+
case StreamWriteException streamWriteException -> Response.status(Response.Status.BAD_REQUEST)
26+
.entity(formatErrorMessage(streamWriteException, "Could not write JSON value")).build();
27+
case JsonMappingException mappingException -> Response.status(Response.Status.INTERNAL_SERVER_ERROR)
28+
.entity("Could not map JSON value: " + mappingException.getMessage()).build();
29+
default -> Response.serverError().entity(exception.getMessage()).build();
30+
};
31+
}
32+
33+
private static String formatErrorMessage(JsonProcessingException e, String message)
34+
{
35+
return "%s: %s at location %s".formatted(message, e.getOriginalMessage(), e.getLocation());
36+
}
37+
}

jaxrs/src/main/java/io/airlift/jaxrs/JaxrsBinder.java

-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package io.airlift.jaxrs;
22

3-
import com.google.common.collect.ImmutableList;
43
import com.google.inject.Binder;
54
import com.google.inject.Key;
65
import com.google.inject.TypeLiteral;
76
import com.google.inject.multibindings.Multibinder;
8-
import org.glassfish.jersey.server.wadl.internal.WadlResource;
9-
import org.glassfish.jersey.server.wadl.processor.OptionsMethodProcessor;
10-
import org.glassfish.jersey.server.wadl.processor.WadlModelProcessor;
11-
12-
import java.util.Collection;
137

148
import static com.google.inject.Scopes.SINGLETON;
159
import static com.google.inject.multibindings.Multibinder.newSetBinder;
@@ -53,9 +47,4 @@ public void bindInstance(Object instance)
5347
{
5448
resourceBinder.addBinding().toInstance(instance);
5549
}
56-
57-
public static Collection<Class<?>> getBuiltinResources()
58-
{
59-
return ImmutableList.of(WadlResource.class, WadlModelProcessor.class, OptionsMethodProcessor.class);
60-
}
6150
}

jaxrs/src/main/java/io/airlift/jaxrs/JaxrsModule.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,10 @@ public JaxrsModule(boolean requireExplicitBindings)
4747
protected void setup(Binder binder)
4848
{
4949
binder.disableCircularProxies();
50-
5150
binder.bind(Servlet.class).to(Key.get(ServletContainer.class));
52-
jaxrsBinder(binder).bind(JsonMapper.class);
53-
jaxrsBinder(binder).bind(SmileMapper.class);
54-
jaxrsBinder(binder).bind(ParsingExceptionMapper.class);
55-
5651
newSetBinder(binder, Object.class, JaxrsResource.class).permitDuplicates();
52+
jaxrsBinder(binder).bind(JsonMapper.class);
53+
jaxrsBinder(binder).bind(JacksonMapper.class);
5754

5855
if (getProperty("tracing.enabled").map(Boolean::parseBoolean).orElse(false)) {
5956
install(new JaxrsTracingModule());

0 commit comments

Comments
 (0)