I need to design REST api and I faced the problem of passing additionl service information to my DTOs.
I have dto like:
public class UserDto implements Serializable {
private Long id;
private String login;
private String firstName;
private String lastName;
private String middleName;
// getters/setters
}
also I have endpoint with list of dtos.
I need to pass AvailableActions with every response. What is the best practice for such case? Is that correct to wrap every result with something like:
public class Wrapper<T> {
private T data;
private List<Action> availableActions;
// getters/setters
}
Any ideas? Any thougths? Thanks in advance.
Yes, enveloping is one widely used technique, how to send metadata with the response. For example JSON API standard uses this approach.
Regarding your code itself. I prefer to generate the API from DTO objects. With JSON API and Java I use Katharsis, with which I just specify the DTO and annotate it in JPA style + create repositories for data retrieval. The wrapping with links are generated automatically (as well as all endpoints - getOne, getAll...). This way I enforce the API standard usage (JSON API) and get rid of all LinkWrappers, PaginationMetadataWrappers etc...
Similar API construction (search_metadata attached to the response object) is used by Twitter, at least in this example.