Decoupling Domain Model and Generation View

I have one domain model with JPA annotations as my application data structure. Now, suppose we have two domain classes with the following fields + getters and setters:

- label
- color

- category
- label
- created
- due


As you can see, the task depends on the Category class. Now I have two REST resources: /api/categories

and /api/tasks

with optional /{id}

URI parameters .

Now when you get or update a Task object through a REST resource, I don't want to send or receive a dependent Category object. I would rather only have the link id in the TASK TASK 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 main domain model through a REST resource, Jackson would also put a Category object in a Task object, which I don't want.

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


source to share

1 answer

You can take a long time, or you can use "automapper". There are several that I have seen, but I cannot recommend them as I have only used AutoMapper in C #.

This question covers equivalents: Automapper for Java



All Articles