Wednesday, March 30, 2016

A simple Json Demo (2)

Building up on the previous demo, I enriched my samples with a method that returns a Json array.

After doing a bit of research, I came up with this solution: my method returns a JsonArray:


...
@GET
@Path("testArray")
@Produces({MediaType.APPLICATION_JSON})
public JsonArray testArray4() throws Exception {

    JsonArray value = Json.createArrayBuilder()
            .add(Json.createObjectBuilder()
                    .add("firstName", "Barak")
                    .add("lastName", "Obama"))
            .add(Json.createObjectBuilder()
                    .add("firstName", "Bill")
                    .add("lastName", "Clinton"))
            .build();
    return value;
}
...

It is available under http://localhost:8080/demoRest/demo/testArray

Note that the deployment occurs using the exploded war of IntelliJ on Glassfish 4.1, which allows defining a custom context root under "/". Otherwise, the *.war file being (oddly) DemoBean_war.war, the URL would be http://localhost:8080/DemoBean_war/demoRest/demo/testArray.

I first made diverse other attempts, which all failed because of the internal binding that takes place during Json processing: using a list of dtos, a list of JsonObject, or a list of dtos wrapped in a GenericEntity: all attempt failed with diverse errors in relations with the libraries available on the server:

java.lang.NoClassDefFoundError: 
  javax/xml/parsers/ParserConfigurationException
java.lang.NoClassDefFoundError: 
  Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper
java.lang.ClassNotFoundException: 
  javax.xml.parsers.ParserConfigurationException not found by org.eclipse.persistence.moxy



The solution using a Json Array proved to be working on Glassfish 4.1.1

I found some information about incompatibilities in Jersey Libs of Glasshfish 4.1.1 on the Jersey Jira, but none helped me solving the problems.

I found a hint in Adam's Bien Blog for returning a list of objects with JAX-RS, but this didn't solve the problems that occured when running on Glassfish. 

I also could not deploy successfully the examples on JBoss, but did not investigate further.