Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using map vs DTO in spring-boot applicatoin [closed]

What is preferred way to accept POST request data in spring-boot application:
either to use a map(key,value) pair to map the post request's body or use a DTO to map the request content.
If someone can elicit the pros and cons of each it will be really helpful.

Code Snippet for same-
Using map:

@PostMapping("/updateBalance")
public ResponseEntity<?> updateBalance(RequestEntity<List<Map<String, Object>>> requestData,HttpServletRequest headerRequest)

Using DTO:

@PostMapping("/updateBalance")
public ResponseEntity<?> updateBalance(List<RequestDTO> requestData,HttpServletRequest headerRequest)
like image 902
arman Avatar asked Jan 18 '26 09:01

arman


1 Answers

If the format of your incoming data is fixed (ie always has the same data items), you use a DTO. This allows to fail fast if the expected format is not matched.

If the incoming data has variable entries, you can use a map and pluck it apart to put the individual items however they need to be handled.

like image 70
daniu Avatar answered Jan 19 '26 22:01

daniu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!