0

I am using Spring MVC for creating a restful API. I have two different API endpoints where I need to serialise same POJO in two different ways. I have illustrated the same below:

Course API

url - /course/{id}

response - {
    "id": "c1234",
    "name": "some-course",
    "institute": {
        "id": "i1234",
        "name": "XYZ College"
    }
}

My Course Pojo is in accordance with the above structure, so the default serialisation works.

class Course {
    private String id;
    private String name;
    private Institute institute;

    //getters and setters follow
}

class Institute {
    private String id;
    private String name;

    //getters and setters follow
}

Now, for another Students API

url - /student/{id}

response - {
    "id":"s1234",
    "name":"Jon Doe",
    "institute": {
        "id": "i1234",
        "name": "XYZ college"
    },
    "course": {
        "id": "c1234",
        "name": "some-course"
    }
}

And my Student class looks like this:

class Student {
    private String id;
    private String name;
    private Course course;

    //getters and setters follow
}

Please note that there is no institute property in Student class because institute can be transitively determined from the course.getInstitute getter. But that ends up in a serialisation structure similar to course API. How can I use a custom serialisation only for the Students API without modifying the POJO Structure.

There are a N number of solutions to this that come to my mind, which is the most elegant and preferred one, I would like to know.

mickeymoon
  • 4,820
  • 5
  • 31
  • 56
  • 2
    First problem I see is that `course` shouldn't be a `String`. Accurate modeling might take care of the issue for you. – chrylis -cautiouslyoptimistic- Feb 07 '16 at 14:22
  • @chrylis Oh.. that was a typo, it is type of `Course` and not `String`. Have edited it. – mickeymoon Feb 07 '16 at 14:30
  • Is this format a specific external requirement? It seems that it makes unnecessary assumptions and could be simplified by just making the REST API conform to the domain model. – chrylis -cautiouslyoptimistic- Feb 07 '16 at 15:03
  • It is intuitive for external world to say student studies in XYZ college than to say student studies in college where a certain course is running(uniquely). So the APIs are more on intuitive modeling. – mickeymoon Feb 07 '16 at 16:08
  • Then perhaps what you ought to use is something like Jackson's ID serialization, where you can tell it to only serialize a particular object (the `Institute`) the first time and then refer to it by ID later. – chrylis -cautiouslyoptimistic- Feb 07 '16 at 19:16
  • If you are using Jackson you could ignore the properties that you don't want just adding this line `.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)` in your _ObjectMapper_ – user2669657 Feb 08 '16 at 00:17

1 Answers1

0

I guess this is the most elegant thing I sorted out which works for me.

So, here is my class Student

class Student {

    private String id;
    private String name;

    @JsonIgnoreProperties({"institute"})
    private Course course;

    //getters and setters

    //Adding one more getter
    public Institute getInstitute() {
        return this.course.getInstitute();
    }
}

This way I am exposing a Java bean property institute through a getter while not holding a reference to it in my Student Object.

mickeymoon
  • 4,820
  • 5
  • 31
  • 56