0

In my REST applications (under GlassFish 4.1.2) I want to convert POJOs to JSON and back again. The examples all make it look easy but I am missing something.

Here is my application:

@ApplicationPath("/")
public class RootApp extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        HashSet set = new HashSet<Class<?>>();
        set.add(HelloWorld.class);
        return set;
    }

    @Override
    public Set<Object> getSingletons() {
        HashSet set = new HashSet<Object>();

        MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
        moxyJsonProvider.setFormattedOutput(true);
        set.add(moxyJsonProvider);

        return set;
    }
}

And here is the Resource:

@Path("helloworld")
public class HelloWorld {

    private static int counter = 1;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getInevitableMessage() {    
        JsonHello hj = new JsonHello("Hello World", counter++);    
        return Response.ok(hj).build();
    }    
}

And last and least is the POJO to convert to and from JSON:

public class JsonHello {

    private int count;
    private String message;

    public JsonHello(String message, int count) {
        this.message = message;
        this.count = count;
    }

    public int count() { return count; }
    public void count(int value) { count = value; }

    public String message() { return message; }
    public void message(String value) { message = value; }
}

I am referring to the tagged answer in this thread. When I attempt to access "/helloworld" it pitches the following exception:

org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper

This application works if the resource just returns a string. There is nothing in the web.xml file since I am letting Glassfish set the application via its decorators.

Any idea what I am missing here?

Community
  • 1
  • 1
AlanObject
  • 9,613
  • 19
  • 86
  • 142
  • How are you handling dependencies? Maven? If so, what dependencies are you using? Manually adding jars? If so, what jars are you using? What versions are you using? What version of Glassfish are you using? Please answer each one of these questions, and edit your post (not in comments) – Paul Samsotha Apr 09 '17 at 00:40
  • @peeskillet This project was generated with Idea 2017.1 and uses GlassFish 4.1.2. It uses the IDE's build system -- not Ant or Maven. The libraries are imported manually and I am using Jackson 2.8.4. I don't know how to tell the IDE that a library is "Provided" (i.e. already part of GlassFish) versus packaged with the application and that may be a problem. – AlanObject Apr 09 '17 at 02:15
  • If you are using Jackson, then why are you trying to register MOXy? – Paul Samsotha Apr 09 '17 at 02:17
  • Check [this out](http://stackoverflow.com/questions/30423776/post-to-jersey-rest-service-getting-error-415-unsupported-media-type/30424031#30424031). Switch the Jersey versions to the version of Jersey you are using, and switch Jackson jars to version of Jackson you want to use. Then register the `JacksonFeature` instead of the MOXy provider. – Paul Samsotha Apr 09 '17 at 02:19
  • @peeskillet I got into MOXy because I am confused by documentation and blog articles like here: http://stackoverflow.com/questions/17568469/jersey-2-0-equivalent-to-pojomappingfeature -- however I really don't have to be using Jackson I just want POJO<->JSON with the minimum fuss. I'll try your suggestion. – AlanObject Apr 09 '17 at 03:19

1 Answers1

1

I ended up solving the problem using the direction that @peeskillet suggested. MOXyJsonProvider is unneeded.

One problem that is hard to address is that almost all the examples on the web assume you are configuring your Servlet with a web.xml file, which I am not. All the configuration I do is from inside the Application object. The Jersey documentation does not make this very clear. What ends up working is this:

@Override
public Set<Class<?>> getClasses() {
    HashSet set = new HashSet<Class<?>>();
    set.add(JacksonFeature.class);
    set.add(MyObjectMapperProvider.class);
    set.add(Home.class);
    set.add(HelloWorld.class);
    return set;
}

At this point the REST resources can produce and consume various POJOs which are transcoded into JSON perfectly and without any effort.

Instead of just deleting this question I will put this answer here in hopes of saving someone the amount of time I spent finding this out.

AlanObject
  • 9,613
  • 19
  • 86
  • 142