Spring Boot

Spring REST Controllers

REST APIs with @RestController, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, ResponseEntity

20 câu hỏi phỏng vấn·
Mid-Level
1

What is the main difference between @RestController and @Controller?

Câu trả lời

@RestController is a composite annotation combining @Controller and @ResponseBody. This means all methods in a @RestController automatically return serialized data (JSON/XML) instead of resolving a view. @RestController is ideal for creating REST APIs, while @Controller is used for traditional web applications with HTML views.

2

What is the role of @RequestBody annotation in a REST method?

Câu trả lời

@RequestBody tells Spring to deserialize the HTTP request body (typically JSON or XML) into a Java object. Spring uses an HttpMessageConverter (like Jackson for JSON) to perform this conversion automatically. This is essential for receiving complex data in POST or PUT requests.

3

Which annotation should be used to map an HTTP DELETE request?

Câu trả lời

@DeleteMapping is a specialized annotation introduced in Spring 4.3 to map HTTP DELETE requests concisely. It's a shortcut for @RequestMapping(method = RequestMethod.DELETE). Spring provides similar annotations for each HTTP method: @GetMapping, @PostMapping, @PutMapping, @PatchMapping.

4

How to extract a path variable from URL in Spring REST?

5

What is the advantage of using ResponseEntity<T> instead of directly returning an object?

+17 câu hỏi phỏng vấn

Nắm vững Spring Boot cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí