3

I do have one domain model with JPA annotations as my application's data structure. Now, let's assume that we have two domain classes with the following fields + getters and setters:

Category
- label
- color

Task
- category
- label
- created
- due

As can be seen, the Task is dependent on the Category class. Now, I have two REST resources: /api/categories and /api/tasks with optional /{id} URI parameters.

Now, when retrieving or updating a Task object through the REST resource, I do not want to send or receive the dependent Category object. I'd rather just have the reference ID within the Task REST object.

{
    "id": 123,
    "category": {"id": 456},
    "label": "Test",
    "created": "2014-01-01T00:00:00",
    "due": "2014-04-05T00:00:00"
}

If, however, I were to expose my core domain model through the REST resource, Jackson would also put the Category object within the Task object which I do not want.

So, I figured it would be a great idea to have a separate REST representation model. What is the best way to decouple and then convert from REST representation objects to domain objects. I already have figured out to use services which are then injected into the REST resource class, but how do I convert between the two models?

Vincent
  • 2,342
  • 1
  • 17
  • 22

1 Answers1

2

You can do it long hand or you can use an "automapper". There are a few I've seen but I can't recommend any as I've only used AutoMapper in c#.

This question deals with what equivalents there are: Automapper for Java

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203