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?
What does @RequestHeader do?
Maps HTTP request headers to a controller method parameter
What are the 2 different ways to use @RequestMapping?
- To map an HTTP request to a controller class
- To map an HTTP request to a controller method
What is the name of the Sring MVC starter?
spring-boot-starter-web
How can you change the port of a Spring MVC application?
application.properties -> server.port = ?
If you want to map HTTP HEAD, OPTIONS, and TRACE requests to a controller method, which annotation should you use?
@RequestMapping(method=?, path=?)
Spring Boot
What bean should you use to instantiate a RestTemplate?
RestTemplateBuilder
Does RestTemplate support streaming?
No. Consider using WebClient
What are the 2 limitations of @ResponseStatus when used on a controller method?
- Overrides status codes that may have been defined inside the controller method
- Only works if the controller method does not throw an exception
What is the main limitation of @ResponseStatus on a custom exception?
Returns an HTML error page making it unsuitable for RESTful applications. Consider using ResponseEntity instead