Day 13. If you're returning Entities from your API, you're leaking your database. I made this mistake early: @GetMapping("/users/{id}") public User getUser(@PathVariable Long id) { return userRepository.findById(id).orElseThrow(); } Looks clean. But it's a security risk. Here’s what you might be exposing without realizing: → passwordHash → roles → internal flags like isAdmin That's not an API. That's your database leaking through JSON. And you probably didn't even notice it. The fix is simple: Return a DTO. @GetMapping("/users/{id}") public UserDTO getUser(@PathVariable Long id) { User user = userRepository.findById(id).orElseThrow(); return new UserDTO(user.getId(), user.getName(), user.getEmail()); } What you actually gain: → Security — control what leaves your server → Stability — DB changes don’t break your API → Clarity — frontend gets exactly what it needs The rule I follow now: → Entities belong to your database → DTOs belong to the outside world → Never mix the two Returning Entities is easy. Designing contracts is what makes you a backend developer. Are you still exposing entities directly? 👇 Drop it below #SpringBoot #Java #BackendDevelopment #DTO #JavaDeveloper
Having a wrapper is always much secure.
If you're returning entities directly, you're either leaking data or avoiding proper design. Manual mapping gave me clarity early on. MapStruct saved me time later. What are you using right now? 👇