users@jersey.java.net

[Jersey] Serializing JSON with Moxy

From: Paul O'Fallon <paul_at_ofallonfamily.com>
Date: Tue, 13 May 2014 07:55:43 -0400

Hello! I'm seeing a difference between Moxy and Jackson for serializing
even simple data structures to JSON. The below code works in Jackson, but
throws an error using the default Moxy implementation. Am I fundamentally
missing something about Moxy?

For this resource:

@Path("/test")
public class MyResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Collection<String> getStrings() {
        Collection<String> results = new ArrayList<String>();
        results.add("foo");
        results.add("bar");
        return(results);
    }

}

And this Main class:

public class Main {
    public static final String BASE_URI = "http://localhost:8080/";

    public static HttpServer startServer() {
        ResourceConfig rc = new ResourceConfig().packages("com.paul");
        return
GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
    }

    public static void main(String[] args) throws IOException {
        final HttpServer server = startServer();
        System.in.read();
        server.stop();
    }
}

Using this Moxy dependency produces a 500 error (with no stack trace
printed to stdout):

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
</dependency>

However, switching to Jackson produces the expected output of ["foo","bar"]

<dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.3.0</version>
    </dependency>

Is there something else I should be configuring/enabling to get the same
results from Moxy?

Thank you!

- Paul