1

In my Spring RESTful Service I have Domain Object Car, all service business logic uses it. These objects are obtained from CarDTO objects which in turn are obtained from several external services using RestTemplate. The questions are:

  1. Is it rational to create separate DTO for each external service if consuming structure is different and map them do domain object by different converters, or it's better to use general DTO and converter?
  2. If my previous suggestions are wrong - what is the pest practice of consuming domain object from several api with different structure?

Car domain object:

public class Car {
private Company company;
private String model;
private Location location;
private Double fuel;
private Double price; 

// getters / setters
}
  • I don't understand how can you have and use one `CarDTO` if the structure of response returned by different external services is different. – Roman-Stop RU aggression in UA May 15 '20 at 12:00
  • I agree with you, let me reformulate my question please. I'm new to REST service development and I'm actually trying to understand the best practical approaches. So if we have several external services - is it **normal approach** to create different DTO's and Converters for each service to subsequently aggregate the received data to common list of domain class objects ? – Dmitriy Mishenyov May 15 '20 at 12:15

1 Answers1

1

There is no hard and fast rule that you should create DTOs.

You can directly use your DB object(Car) instead of in-between DTOs. As per my understanding, if there is everything direct field to field mapped then no need for DTOs.

If we have logic or transformations to perform from Request/Response to convert it to DB objects then DTOs are perfect to use to make code clean. It Decouples persistence models from API models and makes code for maintainable.

For more detail, please go through REST API - DTOs or not?

Devkinandan Chauhan
  • 1,785
  • 1
  • 17
  • 42