Spring Framework Case Studies & Practical Application 3 — Questions and Answers
Question 1: A Spring Boot application must support both REST JSON clients and legacy SOAP clients for the same business logic. What is the recommended architecture?
- Duplicate the service layer for each protocol
- Expose a @RestController and a @Endpoint (Spring-WS) both delegating to a shared @Service (Correct answer)
- Use Spring MVC content negotiation to switch between JSON and XML/SOAP
- Configure two separate Spring Boot applications
Correct answer: Expose a @RestController and a @Endpoint (Spring-WS) both delegating to a shared @Service
Placing the business logic in a shared @Service layer and creating separate protocol-specific controllers/endpoints is the standard separation-of-concerns approach.
Question 2: You observe N+1 query issues in a Spring Data JPA app when loading a list of Orders with their Items. What is the correct fix?
- Annotate the Items collection with @Transient
- Use JOIN FETCH in a JPQL query or a @EntityGraph on the repository method (Correct answer)
- Set spring.jpa.open-in-view=true
- Increase the Hibernate batch size to 1000
Correct answer: Use JOIN FETCH in a JPQL query or a @EntityGraph on the repository method
JOIN FETCH or @EntityGraph eagerly loads associated entities in a single SQL query, eliminating the extra per-row queries that cause the N+1 problem.
Question 3: A financial application requires that two database writes always succeed or fail together across two different DataSources. Which Spring feature enables this?
- @Transactional with REQUIRES_NEW propagation
- JTA (Java Transaction API) with a distributed transaction manager such as Atomikos (Correct answer)
- Spring Data's @Modifying annotation on both repositories
- Two nested @Transactional methods with different DataSources
Correct answer: JTA (Java Transaction API) with a distributed transaction manager such as Atomikos
JTA provides XA (two-phase commit) distributed transactions that span multiple resources, ensuring atomicity across two DataSources.
Question 4: A REST API built with Spring MVC must validate request bodies and return structured error responses for constraint violations. Which combination achieves this?
- @Valid on the @RequestBody parameter and a @RestControllerAdvice with @ExceptionHandler(MethodArgumentNotValidException.class) (Correct answer)
- Manually call the Validator bean inside the controller method
- Use @Validated on the @RequestBody and catch Exception in each endpoint
- Add a javax.servlet.Filter that inspects every request body
Correct answer: @Valid on the @RequestBody parameter and a @RestControllerAdvice with @ExceptionHandler(MethodArgumentNotValidException.class)
@Valid triggers Bean Validation, and a @RestControllerAdvice can intercept MethodArgumentNotValidException to return a consistent error payload.
Question 5: A Spring Boot service behind an API gateway must propagate trace IDs across HTTP calls to downstream services. Which library integrates with Spring automatically?
- Log4j MDC configured manually in each service
- Spring Cloud Sleuth (or Micrometer Tracing in Spring Boot 3) (Correct answer)
- A custom Servlet Filter that copies the X-Trace-Id header
- Logback with a custom appender
Correct answer: Spring Cloud Sleuth (or Micrometer Tracing in Spring Boot 3)
Spring Cloud Sleuth / Micrometer Tracing auto-instruments RestTemplate, WebClient, and messaging to propagate trace and span IDs without manual coding.
Question 6: A development team wants feature flags to toggle new Spring bean implementations without redeploying. Which approach is most idiomatic in Spring?
- Use @ConditionalOnProperty so beans activate based on application properties refreshed via Spring Cloud Config (Correct answer)
- Restart the application with a different JAR for each feature combination
- Use @Primary and manually swap beans at runtime via reflection
- Store the flag in a database and poll it every 60 seconds inside the bean
Correct answer: Use @ConditionalOnProperty so beans activate based on application properties refreshed via Spring Cloud Config
@ConditionalOnProperty combined with Spring Cloud Config's /actuator/refresh allows toggling bean implementations by updating a config property without redeployment.
Question 7: A legacy Spring XML-configured application is being migrated to Java config. What is the safest incremental migration strategy?
- Delete all XML files and rewrite everything in one sprint
- Use @ImportResource to import remaining XML files from a @Configuration class while converting beans incrementally (Correct answer)
- Convert all beans to @Component scanning and delete XML on day one
- Run both XML and Java configs in parallel with duplicate bean definitions
Correct answer: Use @ImportResource to import remaining XML files from a @Configuration class while converting beans incrementally
@ImportResource lets the application mix XML and Java configuration, allowing teams to migrate beans gradually without a big-bang rewrite.
A Spring Boot application must support both REST JSON clients and legacy SOAP clients for the same business logic.
What is the recommended architecture?