Spring Boot

Spring MVC Basics

Spring MVC, MVC architecture, DispatcherServlet, @Controller, ViewResolver, Model-View-Controller pattern

20 pytań z rozmów·
Mid-Level
1

Which annotation transforms a Java class into a Spring MVC controller?

Odpowiedź

@Controller marks a class as a Spring MVC controller that handles HTTP requests. It is a specialization of @Component and allows component scanning to automatically detect the controller. @RestController combines @Controller and @ResponseBody for REST APIs, while @RequestMapping defines URL mappings but doesn't declare a controller by itself.

2

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

Odpowiedź

@RestController combines @Controller and @ResponseBody, meaning all methods automatically return serialized data (JSON/XML) instead of view names. With @Controller, you must add @ResponseBody to each method to return data. @RestController is designed for REST APIs, while @Controller is used for traditional web applications with view rendering.

3

How to map a controller method to an HTTP POST request on /users URL?

Odpowiedź

@PostMapping is the specialized annotation for mapping a method to POST requests on a specific URL. It's a shortcut for @RequestMapping(method = RequestMethod.POST). Other specialized annotations include @GetMapping, @PutMapping, @DeleteMapping, and @PatchMapping. This approach is more readable and concise than @RequestMapping with method attribute.

4

How to retrieve a path variable from a URL like /users/{id}?

5

Which annotation to use to automatically deserialize JSON from HTTP request body?

+17 pytań z rozmów

Opanuj Spring Boot na następną rozmowę

Uzyskaj dostęp do wszystkich pytań, flashcards, testów technicznych, ćwiczeń code review i symulatorów rozmów.

Zacznij za darmo