1

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

user1283068
  • 1,694
  • 4
  • 15
  • 25
  • I had faced this issue a year back. Not able to recall what I did. Will try to find out – user123475 Jul 13 '18 at 09:12
  • I had the same 'issue' with JAXB/XML. The reason was that I was marshalling a field declared `SomeSuperclass x` containing an instance of `SomeDerivedClass`. Understandably, JAXB needed to include in the XML the *actual* type of the object, or on unmarshalling I'd end up with an instance of `SomeSuperclass` with the data lost that was only declared in `SomeDerivedClass` - or even an exception in case `SomeSuperclass` was abstract. See if you have that kind of polymorphism and if you can get rid of marshalling specializations of declared types. – JimmyB Jul 13 '18 at 10:09
  • "javax.ws.rs.core.Response containing an Object" may be the cause because the unmarshaller cannot infer from the declaration of `Response` what kind of object it actually contains. – JimmyB Jul 13 '18 at 10:15
  • Perhaps - I'm just not sure why the marshaller cares so deeply about what *kind* of object it is marshalling? It's just a pojo. The receiver of the json structure has no need to know what the original classname was - or am I missing something. – user1283068 Jul 13 '18 at 15:38

1 Answers1

0

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.

user1283068
  • 1,694
  • 4
  • 15
  • 25