Java Interview Preparation

REST API Development Interview Questions

REST controllers, validation, errors, pagination, JSON, CORS, and file handling.

REST API Development Interview Focus

REST controllers, validation, errors, pagination, JSON, CORS, and file handling.

REST API Development interview questions

Interview questions

20 matching

1. How do you create a REST API in Spring Boot? Beginner

Create a class with @RestController, map methods with @GetMapping, @PostMapping, and related annotations, accept input with @RequestBody or parameters, and return a domain object or ResponseEntity.

2. What is the difference between @RequestMapping and specialized mapping annotations? Beginner

@RequestMapping is the general mapping annotation and can specify path and HTTP method. @GetMapping, @PostMapping, @PutMapping, @PatchMapping, and @DeleteMapping are shorter, clearer specialized forms.

3. What is the difference between @PathVariable and @RequestParam? Beginner

@PathVariable reads a value from the URL path, such as /users/42. @RequestParam reads a query parameter, such as /users?page=2.

4. What is the purpose of @RequestBody? Beginner

@RequestBody tells Spring to read the HTTP request body and convert it, commonly from JSON, into a Java object using an HttpMessageConverter.

5. What is the purpose of @ResponseBody? Beginner

@ResponseBody tells Spring to write a method’s return value to the HTTP response body. @RestController applies this behaviour to all handler methods by default.

6. What is ResponseEntity? Intermediate

ResponseEntity represents the complete HTTP response, including body, status code, and headers. It is useful when an endpoint must control the response precisely.

7. How do you return different HTTP status codes? Beginner

Return ResponseEntity.status(...), use ResponseEntity.ok(), created(), noContent(), or annotate an exception and handler with @ResponseStatus. Choose codes that describe the result clearly.

8. How do you validate request data? Beginner

Add the validation starter, place constraints such as @NotBlank or @Size on a request DTO, and use @Valid in the controller. @Validated also supports validation groups and method-level validation.

9. What is the purpose of @Valid and @Validated? Intermediate

Add the validation starter, place constraints such as @NotBlank or @Size on a request DTO, and use @Valid in the controller. @Validated also supports validation groups and method-level validation.

10. How do you create custom validation annotations? Advanced

Create an annotation with @Constraint, implement ConstraintValidator, and return false when the value is invalid. Add a clear message and apply the annotation to the DTO field or class.

11. How do you handle global exceptions? Intermediate

@RestControllerAdvice defines handlers shared by controllers. @ExceptionHandler maps an exception to a consistent response, so controllers do not repeat try-catch code.

12. What are @ControllerAdvice and @ExceptionHandler? Intermediate

@RestControllerAdvice defines handlers shared by controllers. @ExceptionHandler maps an exception to a consistent response, so controllers do not repeat try-catch code.

13. How do you create a standard error-response structure? Intermediate

Use a stable response containing fields such as timestamp, status, error code, message, path, and validation details. Avoid exposing stack traces or internal database information to clients.

14. How do you implement API versioning? Advanced

Common choices are a version in the path such as /api/v1/users, a request header, or a media type. Pick one strategy and document it consistently for clients.

15. How do you implement pagination and sorting in REST APIs? Intermediate

Accept page, size, and sort parameters, validate their limits, and return a Page or a custom response containing items and paging metadata. Spring Data Pageable can bind these values automatically.

16. How do you enable CORS? Beginner

Configure CORS with @CrossOrigin for a small case or a WebMvcConfigurer or Spring Security configuration for application-wide rules. Allow only trusted origins and required methods.

17. What is content negotiation? Intermediate

Content negotiation lets a client and server agree on a response representation such as JSON or XML. Spring uses request headers, URL patterns, and configured converters to choose the representation.

18. How does Spring Boot convert Java objects to JSON? Beginner

Spring Boot auto-configures Jackson when it is on the classpath. ObjectMapper converts Java objects to JSON and JSON request bodies back to Java objects; annotations or a configured ObjectMapper customize the process.

19. How do you customize Jackson serialization and deserialization? Advanced

Spring Boot auto-configures Jackson when it is on the classpath. ObjectMapper converts Java objects to JSON and JSON request bodies back to Java objects; annotations or a configured ObjectMapper customize the process.

20. How do you handle file uploads and downloads? Intermediate

Accept uploads as MultipartFile and validate size, type, and filename. For downloads, return a Resource or byte stream with suitable content type and Content-Disposition headers. Store files outside the application package.

How to Prepare for Java Technical Interviews

Use this Java technical interview question bank to revise core concepts and practise explaining your decisions clearly. The questions cover Java fundamentals, object-oriented programming, collections, exceptions, multithreading, Spring Boot, Hibernate, databases, and other topics used in real software development interviews.

Start with the fundamentals, then move to scenario-based questions and advanced topics. Filter questions by difficulty to build confidence gradually. A strong answer should define the concept, explain why it matters, and include a practical example when appropriate.

Continue your preparation with the Java tutorials, or explore all interview preparation tracks for managerial and company-focused questions.