Question

I've put some good hours trying to get my grizzly webserver to produce JSON.

Generated from maven archetype using Intellij, details:

groupId untitled3
artifactId  untitled3
version 1.0-SNAPSHOT
archetypeGroupId    org.glassfish.jersey.archetypes
archetypeArtifactId jersey-quickstart-grizzly2
archetypeVersion    2.0-m05

All online examples enable JSON using

rc.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

However, this does not work in jersey 2.x, put method does not exist.

In the application Main class, there is instructions on uncommenting a line of code to get JSON to work. When i uncomment this line of code, the method used does not exist.

public static HttpServer startServer() {
    // create a resource config that scans for JAX-RS resources and providers
    // in untitled2 package
    final ResourceConfig rc = new ResourceConfig().packages("untitled2");

    // uncomment the following line if you want to enable
    // support for JSON on the service (you also have to uncomment
    // dependency on jersey-media-json module in pom.xml)
    // --
    rc.addModule(org.glassfish.jersey.media.json.JsonJaxbModule);

    // create and start a new instance of grizzly http server
    // exposing the Jersey application at BASE_URI
    return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}

When i try to serve a JSON response from a POJO object i get this error:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.example.JsonPost, genericType=class com.example.JsonPost.

I've no idea where to begin to look really. I've googled, plown through documentation and looked through user groups...

Was it helpful?

Solution

Make sure you uncomment the dependency in the POM. Then change the addModule call to this:

rc.addModules(new org.glassfish.jersey.media.json.JsonJaxbModule());

and be sure to include the correct @Produces annotation on your resource:

@Produces(MediaType.APPLICATION_JSON)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top