A note At the time I wrote this answer I assumed that the DTO's you are talking about are request and response objects of the service layer. Thus I use the term DTO in this way and the term resource objects for your 'DTO' objects. I don't want to edit the whole answer right now. Therefore this hint.
where should I be mapping my DAO entities to DTO resources?
In the service layer. Normally the service layer implements use cases and is the transaction boundary.
E.g.
@Transactional
public OrderPlacedTO placeOrder(ShoppingCartTO cart)[
OrderDao orderDao = ...
// implement your use case here
...
}
I wish to add Link elements to the returned resource (for self and related resources), but can't see how Link elements would be resolved if processed in the Service without it having knowledge of the Controller and it's @RequestMapping
You are right. These information is only available in the controller. You can add a resource model, like a normal ui model.
E.g.
public class OrderPlacedRessource extends ResourceSupport {
private Long oderNumber;
public void setOrderNumber(Long orderNumber){ this.orderNumber = orderNumber; }
public Long getOrderNumber() { return this.orderNumber }
}
and in your controller you can then use it and add links
@RequestMapping("/placeOrder")
public @ResponseBody OrderPlacedRessource placeOrderAction(HttpServletRequest request){
OrderService service = ...;
ShoppingCartTO cart = ...;
OrderPlacedTO orderPlacedTO = service.placeOrder(cart);
OrderPlacedRessource orderPlacedRes = new OrderPlacedModel();
orderPlacedRes.setOrderNumber(orderPlacedTO.getOrderNumber());
// since orderPlacedRes is a RessourceSupport we can add links
// Use the HttpServletRequest to build your links
Link link = new Link("http://localhost:8080/something");
orderPlacedRes.addLink(link);
return orderPlacedRes;
}
PS: Building links is easier if you use a org.springframework.hateoas.mvc.ControllerLinkBuilder
. E.g
orderPlacedRes.add(linkTo(methodOn(YourController.class).orderOverview(userId)).withSelfRel());
See Building a Hypermedia-Driven RESTful Web Service for details.
OrderDao
), DTO (e.g.OrderPlacedTo
) and Resource (e.g.OrderPlacedResource
)? This does address my concerns about the service being controller-aware (and vice versa), but as the data model grows this could spiral out of control. – Messroom