Spring Web Flashcards
What does @RestController do?
Annotates a controller class with:
1. @Controller
2. @ResponseBody
- What is a servlet?
- What is Spring MVC’s main servlet called?
- A servlet is a component of a servlet container used to handle HTTP requests and responses
- DispatcherServlet
What is Spring MVC?
A Spring framework for building web applications
Spring MVC
Describe the lifecycle of an HTTP request
What does @ResponseBody do?
Maps the return value of a controller method to an HTTP response body
What does @RequestParam
do?
Maps HTTP query parameters to controller method parameters. For example:
// localhost:8080/?name=gianmarco @GetMapping public String getName(@RequestParam String name) { return name; } // returns gianmarco
What does @PathVariable do?
Maps a URI template variable to a controller method parameter. For example:
// localhost:8080/1 @GetMapping("/{id}") public int getId(@PathVariable int id) { return id; } // returns 1
Why does Spring recommend using ResponseEntity instead of HttpServletResponse?
ResponseEntity can be used in a test environment
Successful status codes for:
- GET
- POST
- PUT
- PATCH
- DELETE
- 200
- 201
- 204
- 204
- 204
- How does Spring MVC determine which message converter to use for an HTTP request?
- How does Spring MVC determine which message converter to use for an HTTP response?
-
Content-Type
header -
Accept
header
When should a “Location” header be sent back in an HTTP response?
Whenever an HTTP POST request is successful
What class does Spring recommend for building URIs?
ServletUriComponentsBuilder
What does @ExceptionHandler do?
It marks a controller method as an exception handler. For example:
@ExceptionHandler(Exception.class) public ProblemDetail handleExceptions() { return ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR); }
True or False
ResponseStatusException(…) can be thrown inside a controller method
True
Can a controller method declare a Pageable parameter?