jersey icon indicating copy to clipboard operation
jersey copied to clipboard

JSON-Jackson does not handle resource returning CompletionStage<X>

Open computerlove opened this issue 7 years ago • 2 comments

It seems like jersey-media-json-jackson is not up to date with the latest JAX-RS 2.1 features. I tried creating the following resource method:

@GET
@Produces({"application/json" })
@Path("async")
public CompletionStage<Hello> asyncHello(@NotNull @PathParam(value = "lang") String lang,
                                         @NotNull @Size(min = 2) @QueryParam("yo") String greet){
        return CompletableFuture.supplyAsync(() -> new Hello(lang, greet);
}

This fails with stack trace

java.lang.IllegalArgumentException: Class org.kantega.reststop.helloworld.jaxrs.Hello not subtype of [simple type, class java.util.concurrent.CompletionStage<org.kantega.reststop.helloworld.jaxrs.Hello>]
	at com.fasterxml.jackson.databind.type.TypeFactory.constructSpecializedType(TypeFactory.java:359)
	at org.glassfish.jersey.jackson.internal.jackson.jaxrs.base.ProviderBase.writeTo(ProviderBase.java:624)
	at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:266)

Returning a plain String with text/plain works as expected.

Just as a test I build my own version of Jersey with some additional code in org.glassfish.jersey.jackson.internal.jackson.jaxrs.base.ProviderBase.writeTo(...):

TypeFactory typeFactory = writer.getTypeFactory();

String typeName = genericType.getTypeName();
if(typeName.contains(CompletionStage.class.getName())) {
    Type[] actualTypeArguments = ((ParameterizedTypeImpl) genericType).getActualTypeArguments();
    genericType = actualTypeArguments[0];
}

JavaType baseType = typeFactory.constructType(genericType);

The middle part unwrapping the CompletionStage is new. This works.

So, have I missed something, or is the CompletionStage support not complete?

computerlove avatar Sep 18 '17 10:09 computerlove

Any traction on fixing this? It's one of the very first thing you try when switching to JAX-RS 2.1 and it's sad to see it break right away 😅

dandew avatar Feb 14 '18 02:02 dandew

As far as I see it should'nt be a big task to fix. Not sure if the above fix/hack is the correct place to handle this. It would be nice if someone could comment on if this is a acceptable solution, and if not point us in the general area where it should be handled.

computerlove avatar Feb 14 '18 06:02 computerlove