Search code examples
javarestjersey-2.0moxy

Marshalling java POJO's with Jersey and Moxy


I have a REST based web application that basically processes requests and sends back responses i JSON format. Pretty basic stuff.

For this I'm using these dependencies, just to clarify:

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.23.2</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>2.23.2</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.23.2</version>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.moxy</artifactId>
    <version>2.7.2</version>
</dependency>

Rather than diving into code let me just outline the basic premises here:

  • Jersey2 is being used for REST processing
  • The result returned by my method (which @Produces json) is a javax.ws.rs.core.Response containing an Object
  • Said object class is annotated with @XmlAccessorType(XmlAccessType.FIELD)

Not a lot of detail but hopefully we won't need more.

The thing I'm having a problem with is the json being returned, specifically one single field called "type". Here's an example:

{"type":"getPersonResult","processingTime":19,"person":{"stamPersonPID":170715,"cpr":"1223344556","fornavn":"Ulrik","efternavn":"Jørgensen","foedselsdato":"1901-01-24T00:00:00+01:00","vejnavn":"Vermindsgade","husnummer":"382C","postnummer":"1160","postDistrikt":"København K"},"encryptedPersonIdentifier":"er8xGcWCviTyP2LeIquzYA%3D%3D"}

See that first "type" field? Ok, how do I get rid of that? It's not part of my model (object being returned), it's like Moxy is appending that automatically.

I tried adding a "type" field and annotating it with @XmlTransient but Moxy ignores that and outputs a type field anyway.

What can I do to avoid that field being present in my json response?

-Michael


Solution

  • By following suggestions in this answer I managed to remove the type field.

    Specifically I added the @XmlType(name="") annotation to my response object. This solved the problem.