Java – Is a class representing a JSON response a DTO, a domain object, or something else

design-principlesdomain-objectsdtojavapackages

Consider a restful service in Spring MVC. I am wondering how to package my response classes, i.e. how to name the containing package.

First I thought of them as being domain objects. But they are actually a bit different, because they wrap my true domain objects like this:

{
    header: {
        // ...
    },
    domainObjectSpecificKey: {
        // domain object properties
    }
}

The response class is mapped to the top-level JSON object. So then I thought they may be DTOs actually — but as far as I understand DTOs are used between application layers, and not for "output" objects to be used between separate applications.

So my questions is: does this kind of object have a special name? If yes, what's that? If not, can we definitely state that it is NOT a DTO or a domain object, i.e. should I name the package differently or can I use the one of that seems better to my liking?

Best Answer

I would consider a class representing a JSON response to be a Data Contract. Keeping it separate from your DTOs and Domain Objects also keeps the clients consuming your service decoupled from the rest of your system.

Related Topic