Yohanes AI
Yohanes AI

Reputation: 3621

Difference JSON ouput within glassfish version

Using Jersey with the same code and testing in Glassfish 3.1.2 and 4.1.2 have different result output. Here is the code :

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<Lenovo> getList(@DefaultValue("0") @QueryParam("limit") final int limit,
                                    @DefaultValue("0") @QueryParam("offset") final int offset,
                                    @DefaultValue("0") @QueryParam("id") final int id,
                                    @DefaultValue("") @QueryParam("series") final String series) {

        DBBase obj = new Lenovo(id, series);
        DBSearch dbs = new DBSearch(limit, offset, obj);

        return bl.getList(dbs);
    }

JSON output from glassfish 3.1.2 :

    {
      "Lenovo": [
        {
          "id": "1",
          "series": "X220"
        },
        {
          "id": "12",
          "series": "X230"
        } 
      ]
    }

JSON Output from Glassfish 4.1.2 :

    [
      { 
        "id": "1",
        "series": "x220"
      },
      { 
        "id": "2",
        "series": "x230"
      }
    ]

Get data from database and return object.

    ArrayList<Agama> rtn = new ArrayList<Lenovo>();

    ResultSet rs = stmt.executeQuery(sql.toString());

        while (rs.next()) {
            rtn.add(new Lenovo(rs));
        }

    return rtn;

Lenovo Object :

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(namespace="Lenovo")
    @XmlType(name = "Lenovo", propOrder = { 
        "id", 
        "series" 
    })

    public class Lenovo extends DBBase {
        int id;
        String series;

        private void setValues(ResultSet rs) {
            try {
                id = rs.getInt("id");
                series = rs.getString("series");
            } catch (SQLException ex) {
                ErisHelper.logger.error("Err>" + ex.getMessage(), ex);
            }
        }

    }

I've tried to using java platform JDK 1.6, 1.7 both of GLassfish and got the same result. How to get same JSON result output using same code in difference Glassfish version. Thanks.

Upvotes: 1

Views: 217

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209132

It's because they are using different serializers. Glassfish 3 is using some internal serializer. You can configure it to use Jackson 1, with just a web.xml configuration swtich, but still Glassfish 4 used MOXy by default, as its serializer.

Best thing to do, if you want the same behavior, is to just use the same serializer. You can add the following (Jackson 2) dependency

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

If you're using web.xml for configuration, then just use the package scanning to pick up the providers.

In Glassfish 3

<init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>com.fasterxml.jackson.jaxrs.json</param-value>
</init-param>

In Glassfish 4

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.fasterxml.jackson.jaxrs.json</param-value>
</init-param>

If you want the exception mappers, then you should also add the following package

<param-value>
    com.fasterxml.jackson.jaxrs.json,
    com.fasterxml.jackson.jaxrs.base   <----
</param-value>

If you're not using web.xml, ie. you're using an Application subclass, you should register the JacksonJaxbJsonProvider, and if you want the exception mappers, JsonMappingExceptionMapper, JsonParseExceptionMapper.

The last thing you need to do is to disabled MOXy in Glassfish 4. For this you can just set a property. In web.xml you can do

<init-param>
    <param-name>jersey.config.disableMoxyJson</param-name>
    <param-value>true</param-value>
</init-param>

If you're using an Application subclass, you can do

class MyApplication extends Application {
    @Override
    public Map<String, String> getProperties() {
        Map<String, String> props = new HashMap<>();
        props.put("jersey.config.disableMoxyJson", "true");
        return props;
    }
}

Upvotes: 1

Related Questions