Scrivio Interview library

REST controllers

27 questions in 7 subtopics.

@RestController and mappings

ConceptFoundations5 min read
What does @RestController add compared to @Controller, and what do @GetMapping and @RequestMapping do?
CodingFoundations5 min read
Write a controller with GET /orders/{id} returning JSON and POST /orders accepting JSON and returning 201 with a Location header.
ScenarioIntermediate5 min read
In code review you see controllers that call repositories directly, return JPA entities and contain business rules. What would you change and why?
LeadershipAdvanced5 min read
Teams build REST controllers in very different styles: paths, status codes, paging, errors. How would you create consistency across 80 services?
LeadershipPrincipal5 min read
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?
LeadershipPrincipal6 min read
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

ConceptFoundations5 min read
Compare @PathVariable and @RequestParam. What happens when a required parameter is missing or has the wrong type?
DebuggingFoundations5 min read
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?
CodingIntermediate6 min read
Design the query parameters for GET /orders with filtering by status, date range, sorting and paging, and show how you would bind them.
LeadershipPrincipal6 min read
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

DebuggingFoundations6 min read
A POST with a JSON body returns 415 Unsupported Media Type. What are the likely causes?
ConceptIntermediate6 min read
Why should a controller not bind request bodies directly to JPA entities? Give a concrete risk.
ScenarioIntermediate7 min read
A client uploads 50 MB JSON payloads to a POST endpoint and the service runs out of memory. How would you protect the service?
ScenarioAdvanced7 min read
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

CodingFoundations6 min read
Return 404 when an order is not found, 204 after a delete, and 409 when a duplicate is created. Show the code options.
DesignIntermediate5 min read
When would you return 200, 201, 202 and 204 from POST endpoints? Give an example for each.
LeadershipPrincipal6 min read
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

ConceptFoundations6 min read
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?
DebuggingIntermediate6 min read
After adding a dependency that brings jackson-dataformat-xml, some clients start receiving XML instead of JSON. Why?
DesignAdvanced7 min read
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

CodingFoundations5 min read
Read a header X-Request-Id and a cookie session_hint in a controller method, and set a secure cookie in the response.
CodingIntermediate7 min read
Add ETag support so that clients get 304 Not Modified for unchanged orders. Show two approaches.
ScenarioIntermediate6 min read
A browser SPA on app.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?
DesignAdvanced7 min read
Design the standard for cross-cutting HTTP headers across services: correlation ids, idempotency keys, caching and rate limit headers.

HTTP methods and OPTIONS requests

ConceptFoundations6 min read
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?
DebuggingIntermediate6 min read
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.
DesignAdvanced6 min read
Define the API guideline for HTTP method use and idempotency across your company's services, including how clients safely retry POST requests.