Search code examples
javaspring-bootdesign-patternsarchitecturedto

Managing DTO and BO in a project calling an API


I'm working on a backend layer of a webapp in Java which is essentially an RESTful API (using Spring Boot). Within my backend I'm calling another API (essentially my database) to retrieve and combine data. Current my workflow for handling a GET request looks the following

  1. Receive request within my API
  2. Call the other API (database), and map it's JSON response to a DTO
  3. Convert DTO to BO to perform logic on it
  4. Return BO via spring boot (which turns its fields into a JSON body and returns this)

My question is
Would following the DTO/BO pattern, "require" me to turn my BO into a second DTO, containing the data to be transferred?

Essentially this would add a step to my list above, between 3 and 4:
---> ...
---> 3.5. Convert BO to new DTO
---> 4. Return DTO via sprint boot

The reason I haven't done this is because it seems a bit intensive to convert objects twice, however to DTO/BO pattern would give me control over which exact fields should be returned.


Solution

  • The JSON body that spring returns is itself a DTO. If you are unhappy with that representation, then yes, you should take control and create your own DTO for the transfer out of your application.

    Most likely this would be for the purpose of information hiding. If there are fields that outside applications have no business knowing (internal ids, etc), then you will want to create an object that hides those things.

    If your BO happens to align with what you want to share, then adding another layer of mapping that doesn't change anything adds no value above what spring is already accomplishing for you.