1

I have a bit a difficult situation. I am using jersey to provide REST services.

In my runtime I do this with version: 1.16.0:

restClasspath(
'org.codehaus.jackson:jackson-core-asl:1.9.2',
'org.codehaus.jackson:jackson-mapper-asl:1.9.2',
'com.sun.jersey:jersey-core:1.16.0',
'com.sun.jersey:jersey-server:1.16.0',
)

This is my resource class:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import ....model.DecisionTree;
import ...model.TestDataProvider;

@Path("new_rest")
public class NewPublicAPIRestResource {

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("ppp/{param}")
  public Response getHello(@PathParam("param") String treeName) {
      DecisionTree tree = TestDataProvider.createDecisionTree();
      return Response.ok(tree).build();
  }  
}

This works and I get a json response as expected.

Now I found about jersey-testframework which provides an in memory server for integration tests.

Unfortunately this only seems to work with jersey 2.x, so I added an extra gradle setting for my tests:

testClasspath(
'org.glassfish.jersey.core:jersey-common:2.7',
'org.glassfish.jersey.core:jersey-server:2.7',
'org.glassfish.jersey.core:jersey-client:2.7',
'org.glassfish.hk2:hk2:2.2.0',
'org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-inmemory:2.7',    
 )

And this is my test class:

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;    
import ...resource.NewPublicAPIRestResource;

public class ConditionNodeTest extends JerseyTest{

    @Override
    protected Application configure() {
        return new ResourceConfig(NewPublicAPIRestResource.class);
    }

    @Test
    public void test() {
        System.out.println(target("new_rest/ppp/param").request().get(String.class));
    }
}

This setup does work, as long as my resource returns String. However, as soon as I return a Response, as shown in my code here, I get this error:

javax.ws.rs.ProcessingException: Server-side request processing failed with an error.
    ...
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296)
    at ....model.ConditionNodeTest.test(ConditionNodeTest.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    ...
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    ...
Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class ....model.DecisionTree, genericType=class ....model.DecisionTree.
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)
    at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:571)
    at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:378)
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:368)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:262)
    ... 44 more

From what I can tell I am missing some provider that parses my classes into json, but I just cannot figure out which one. I tried adding a few libs to my classpath like:

'org.glassfish.jersey.media:jersey-media-jaxb:2.16'
'org.glassfish.jersey.media:jersey-media-json-jackson:2.7

and several others I cannot remember, but did not get it working.

Any ideas what I am missing here?

Thanks, Sven

sveri
  • 1,372
  • 1
  • 13
  • 28

1 Answers1

1

This is correct

'org.glassfish.jersey.media:jersey-media-json-jackson:2.7'

But up until Jersey 2.9, we still need to register the JacksonFeature with the application (as mentioned here). So just do

@Override
protected Application configure() {
    return new ResourceConfig(NewPublicAPIRestResource.class)
            .register(JacksonFeature.class);
}

Oh and BTW, Jersey 1.x also has the Jersey Test Framework. It's just the API is a little different, but it still functions the same. See example here and here, and here is in-memory provider. But anyway, I recommend sticking with Jersey 2.x.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Awesome, that works, thank you. I didn't see that I had to register the feature anywhere, seems like I missed that from the docs. – sveri Sep 08 '15 at 18:45
  • Hi, I have the same problem. I am trying to use jersey test framework to check for a situation where my service gives BAD REQUEST. I am actually using a custom exception and mapping this exception to a ExceptionMapper. When I run the service on my JBoss, it works fine. But when I run it in my integration test. It gives the javax.ws.rs.ProcessingException: Server-side request processing failed with an error message. Any help on this, please?? – immzi Feb 04 '16 at 10:04
  • @immzi Please post another question on SO. – Paul Samsotha Feb 04 '16 at 10:07