0

I'm trying to create a very simple example rest service that produces JSON. My understanding is that Jersey should auto discover the Jackson libraries (if they're on the classpath) and the JSON marshalling should happen automatically.

The following error is reported when I call GET. Am I using the appropriate dependencies for this to work or am I missing something more fundamental?

SEVERE: MessageBodyWriter not found for media type=application/json, type=class jsonex.Person, genericType=class jsonex.Person.

Dependencies

I'm using Gradle with the gretty plugin applied and these dependencies:

dependencies {
    compile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2+'
    runtime group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.23'
    runtime group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.7'
}

Application class

package jsonex;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.Collections;
import java.util.Set;

@ApplicationPath ("services")
public class PersonApp extends Application {

    @Override
    public Set<Object> getSingletons() {
        return Collections.singleton(new PersonService());
    }
}

Service class

package jsonex;

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

@Path("people")
public class PersonService {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Person getPerson() {
        Person me = new Person();
        me.setFirstName("Dave");
        me.setLastName("R");
        return me;
    }
}

POJO

package jsonex;

public class Person {
    String firstName, lastName;   

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Thanks!

daver
  • 158
  • 1
  • 6

1 Answers1

2

In order to consume/produce JSON in Jersey with Jackson you must register the Jackson feature (JacksonFeature) in your application. It's not auto discoverable as some other Jersey features.

After adding the jersey-media-json-jackson module to your dependencies, register the JacksonFeature in your Application / ResourceConfig subclass:

@ApplicationPath("/api")
public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(JacksonFeature.class);
        return classes;
    }
}
@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        register(JacksonFeature.class);
    }
}

The steps to use Jackson as a JSON provider for Jersey are fully described in this answer. For more details, also check the Jersey documentation.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • 1
    _"It's not auto discoverable as some other Jersey features."_ - Not true. What's happening is that by directly extending the `Application` class, this voids an "discovering" of resources and providers. So ultimately the OP is disabling the automatic registration of the `JerseyFeature`. So your solution to explicitly register it is correct, just the details is a bit incorrect :-) But with ResourceConfig, you won't need to register it, as it does not disable the auto-discovery – Paul Samsotha Mar 26 '17 at 09:54
  • Thanks this is the information I was missing, unfortunately I still can't get it to work without manually registering JerseyFeature? I'm now using ResourceConfig and it works fine if I register JerseyFeature but I get the same error if I don't. I would like to understand what I'm missing for autodiscovery to work. – daver Mar 26 '17 at 10:34
  • @daver it's your version. auto-discovery isn't included until 2.9 or 2.10. You should be using the same version as your `jersey-container-servlet` anyway – Paul Samsotha Mar 26 '17 at 10:39