Learning SpringBoot at Enet Company

Sep 15, 2024

From Chaos to Structure

When I first started learning SpringBoot, my code was a mess. Controllers mixed with business logic, entities exposed directly to frontend, no security patterns.

Everything changed when I joined Enet company. They showed me how real enterprise applications are built.

The Professional Way

Proper Layered Architecture

src/main/java/com/enet/project/
├── entity/          # Database entities
├── dto/             # Data Transfer Objects  
├── mapper/          # Entity  DTO conversion
├── repository/      # Data access layer
├── service/         # Business logic
└── controller/      # REST endpoints

Entity vs DTO Separation

Never expose entities directly! Always use DTOs:

// Entity (backend only)
@Entity
public class User {
    @Id
    private Long id;
    private String password; // Sensitive!
}

// DTO (safe for frontend)
public class UserResponseDTO {
    private Long id;
    private String username;
    // No password!
}

Clean Service Layer

@Service
@Transactional
public class UserService {
    private final UserRepository userRepository;
    private final UserMapper userMapper;
    
    public List<UserResponseDTO> getAllUsers() {
        return userRepository.findAll()
            .stream()
            .map(userMapper::toDTO)
            .collect(toList());
    }
}

Security Implementation

JWT Authentication

@Component
public class JwtUtils {
    public String generateToken(UserDetails user) {
        return Jwts.builder()
            .setSubject(user.getUsername())
            .setExpiration(new Date(System.currentTimeMillis() + 86400000))
            .signWith(SignatureAlgorithm.HS512, secret)
            .compact();
    }
}

OAuth2 Integration

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.oauth2Login()
            .userInfoEndpoint()
            .userService(customOAuth2UserService);
    }
}

What I Learned

  • Clean Architecture: Proper separation of concerns
  • Security First: JWT, OAuth2 authentication
  • Testing: Unit and integration tests
  • Code Quality: Reviews and best practices

Working at Enet taught me the difference between "code that works" and "professional code."

Huynh Thanh Loc