REST controllers
27 questions in 7 subtopics.
@RestController and mappings
What does@RestController add compared to @Controller, and what do @GetMapping and @RequestMapping do?Write a controller with GET /orders/{id} returning JSON and POST /orders accepting JSON and returning 201 with a Location header.In code review you see controllers that call repositories directly, return JPA entities and contain business rules. What would you change and why?Teams build REST controllers in very different styles: paths, status codes, paging, errors. How would you create consistency across 80 services?Over twenty years, Java web APIs moved from SOAP to REST and now include GraphQL and gRPC. How do you decide which API styles your organisation supports?REST controllers in your company often contain logic that should belong to the domain, making it hard to add other channels like messaging or GraphQL. How would you change the architecture over the coming years?Path variables and request parameters
Compare@PathVariable and @RequestParam. What happens when a required parameter is missing or has the wrong type?After upgrading to Spring Framework 6.1, @PathVariable long id without a name fails with an error that the parameter name is not available. Why?Design the query parameters for GET /orders with filtering by status, date range, sorting and paging, and show how you would bind them.API-first design with OpenAPI and code-first design with annotations both exist in your company. Which would you choose as the long-term standard and why?Request and response bodies
A POST with a JSON body returns 415 Unsupported Media Type. What are the likely causes?Why should a controller not bind request bodies directly to JPA entities? Give a concrete risk.A client uploads 50 MB JSON payloads to a POST endpoint and the service runs out of memory. How would you protect the service?A public API returns JPA entities directly in 30 endpoints, and adding a field to an entity has leaked internal data twice. How would you fix this across the codebase?ResponseEntity and status codes
Return 404 when an order is not found, 204 after a delete, and 409 when a duplicate is created. Show the code options.When would you return 200, 201, 202 and 204 from POST endpoints? Give an example for each.Your public APIs are used by partners for years. How do you decide when an API can be retired, and how do you handle breaking changes over a long time?Content negotiation
How does Spring decide whether to return JSON or XML for GET /orders/1, and what happens if the client asks for a format the server cannot produce?After adding a dependency that brings jackson-dataformat-xml, some clients start receiving XML instead of JSON. Why?Teams want to version REST APIs differently: path, header, media type. With Spring Framework 7 offering API versioning, how would you set one standard and roll it out?Headers and cookies
Read a header X-Request-Id and a cookie session_hint in a controller method, and set a secure cookie in the response.Add ETag support so that clients get 304 Not Modified for unchanged orders. Show two approaches.A browser SPA onapp.example.com calls an API on api.example.com and requests fail with CORS errors only for PUT with JSON. What is happening and how do you configure Spring?Design the standard for cross-cutting HTTP headers across services: correlation ids, idempotency keys, caching and rate limit headers.HTTP methods and OPTIONS requests
Which HTTP methods are safe and which are idempotent? How does Spring MVC handle HEAD and OPTIONS requests for a controller that only declares@GetMapping and @PostMapping?A browser app gets a CORS error on a PUT request. The network tab shows the OPTIONS preflight returning 401 from a Spring Boot API protected by Spring Security, while GET works. Explain and fix it.Define the API guideline for HTTP method use and idempotency across your company's services, including how clients safely retry POST requests.No question matches these choices.