Neilos
Neilos

Reputation: 2746

Glassfish JAX-RS serialising POJO to JSON fails

I'm using Glassfish 4.1.2

I have a jax-rs endpoint that I want to return a JSON object serialised from my POJO entity.

The POJO:

@XmlRootElement
public class MyObj {

    public MyObj() {}

    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

The resource:

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

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public MyObj getMyObject() {
        MyObj myObj = new MyObj();
        myObj.setValue("Some Value");
        return myObj;
    }

}

Produces two exceptions, The first is thrown when the resource is first accessed after the server is started:

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: javax/xml/parsers/ParserConfigurationException

All subsequent requests produce the exception:

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

However, if I get the result as an XML (i.e. I change to @Produces(MediaType.APPLICATION_XML)) it works without exception and the xml is returned no problems.

My request headers accept both xml and json.

UPDATE

My project's dependencies are:

javax:javaee-api:7.0
com.lambdaworks:scrypt:1.4.0
commons-codec:commons-codec:1.10
org.apache.commons:commons-lang3:3.6

I've basically stripped this project down to its bare bones.

UPDATE 2

Following advice from @peeskillet by adding jersey.config.server.disableMoxyJson, true to the Application properties map does kind of work:

@ApplicationPath("")
public class MyServer extends Application {

    @Override
    public Map<String, Object> getProperties() {
        Map<String, Object> map = new HashMap<>();
        map.put("jersey.config.server.disableMoxyJson", true);
        return map;
    }

}

The initial request fails with:

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector

BUT then all subsequent requests do work and I can get the JSON in the respose.

I am a little concerned though that it initially fails.

Upvotes: 0

Views: 1187

Answers (1)

Neilos
Neilos

Reputation: 2746

I couldn't get this working correctly with Glassfish 4.1.2, I switched to using WildFly 10.1.0.Final and it worked right away.

UPDATE

Coming back to this I managed to get Glassfish working with Jackson.

It seems that java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector is because Glassfish 4.1.2 doesn't come bundled with a jackson-module-jaxb-annotations.jar.

The solution is to add the jackson-module-jaxb-annotations.jar to <glassfish install>/glasfish4/glassfish/modules

Then disable Moxy using:

@ApplicationPath("")

public class MyServer extends Application {

    @Override
    public Map<String, Object> getProperties() {
        Map<String, Object> map = new HashMap<>();
        map.put("jersey.config.server.disableMoxyJson", true);
        return map;
    }

}

Upvotes: 1

Related Questions