REST API - DTOs or not?
<p>I am currently creating a REST-API for a project and have been reading article upon article about best practices. Many seem to be against DTOs and simply just expose the domain model, while others seem to think DTOs (or User Models or whatever you want to call it) are bad practice. Personally, I thought that <a href="https://www.ibm.com/developerworks/community/blogs/barcia/entry/data_models_for_api_s_are_not_canonical_or_domain_models_they_are_your_user_model?lang=en" rel="noreferrer">this article</a> made a lot of sense.</p>

<p>However, I also understand the drawbacks of DTOs with all the extra mapping code, domain models that might be 100% identical to their DTO-counterpart and so on. </p>

<p>Our API is mostly created so that other clients may consume data, however if we do it right we would also like to use it for our own web GUI if possible. </p>

<p>The thing is that we might not want to expose all the domain data to the other client users. Much of the data will only make sense in our own web application. Also, we might not want to expose all data about an object in all scenarios, especially relationships to other objects and so on. For example, if we expose a list of a particular object we would not necessarily want to expose the entire object hierarchy; so that the object's children will not be exposed, but can be discovered through links (hateoas). </p>

<p>How should I go about solving this problem? I was thinking about using Jackson mixins on our domain models to control what data would be exposed given different scenarios. Or should we just use DTOs all the way - even given its drawbacks and controversy? </p>