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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With