Code Explainers

Java code explainers

java
package com.example.inventory;
 
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;

Reading and writing CSV with Commons CSV

csv-parsing file-io try-with-resources
Intermediate 7 steps
java
package com.example.security;
 
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

Salted password hashing with PBKDF2 in Java

password-hashing pbkdf2 salting
Intermediate 7 steps
java
package com.example.orders.repository;
 
import com.example.orders.domain.Order;
import org.springframework.data.jpa.repository.JpaRepository;

Projection interfaces in Spring Data JPA

projections jpql aggregation
Intermediate 7 steps
java
@RefreshScope
@Component
public class FeatureSettings {
 

Refreshable feature settings in Spring

caching configuration dependency-injection
Intermediate 7 steps
java
public final class ZipExtractor {
 
    private final Path targetDir;
 

Extracting a zip safely in Java

path-traversal streams file-io
Intermediate 8 steps
java
@Entity
@Table(name = "invoices")
@FilterDef(name = "tenantFilter", parameters = @ParamDef(name = "tenantId", type = String.class))
@Filter(name = "tenantFilter", condition = "tenant_id = :tenantId")

Multi-tenant row filtering with Hibernate in Spring

multi-tenancy hibernate filters request interceptor
Advanced 7 steps
java
@Configuration
@EnableIntegration
public class OrderProcessingFlow {
 

Building an order pipeline with Spring Integration

messaging pipeline enterprise integration patterns
Intermediate 9 steps
java
public OrderConfirmation submitOrder(Order order) throws IOException, InterruptedException {
    String payload = objectMapper.writeValueAsString(order);
 
    HttpRequest request = HttpRequest.newBuilder()

Posting an order with Java's HttpClient

http-client json-serialization error-handling
Intermediate 6 steps
java
package com.acme.billing.config;
 
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationVersion;

Customizing Flyway migrations in Spring

database-migrations flyway dependency-injection
Intermediate 6 steps
java
package com.example.web.convert;
 
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

A custom ISO-week Converter in Spring

type conversion value object immutability
Intermediate 5 steps
java
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;

Streaming a file download with Java's HttpClient

http-client streaming file-io
Intermediate 6 steps
java
@RestController
@RequestMapping("/api/documents")
@RequiredArgsConstructor
public class DocumentController {

Method-level authorization with @PreAuthorize in Spring

authorization spel rest-api
Intermediate 7 steps
java
@WebMvcTest(OrderController.class)
class OrderControllerTest {
 
    @Autowired

Slicing the web layer with @WebMvcTest in Spring

testing mocking web-layer
Intermediate 9 steps
java
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
 
    @EntityGraph(attributePaths = {"customer", "lineItems", "lineItems.product"})

Killing N+1 queries with @EntityGraph in Spring

orm fetching n+1-problem
Intermediate 5 steps
java
@RestController
@RequestMapping("/api/orders")
public class OrderController {
 

Content-negotiated API versioning in Spring

api-versioning content-negotiation rest
Intermediate 7 steps
java
@Service
public class PaymentGatewayClient {
 
    private static final Logger log = LoggerFactory.getLogger(PaymentGatewayClient.class);

Resilient payment calls in Spring

circuit breaker retry fault tolerance
Intermediate 7 steps
java
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface CurrentUser {
}

Injecting the current user in Spring MVC

annotations dependency-injection security
Intermediate 8 steps
java
public final class TokenBucketRateLimiter {
 
    private final long capacity;
    private final double refillTokensPerNano;

How a token bucket rate limiter works

rate-limiting concurrency token-bucket
Intermediate 8 steps