What does @RestController do?
Annotates a controller class with:
1. @Controller
2. @ResponseBody
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 gianmarcoWhat 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 1Why does Spring recommend using ResponseEntity instead of HttpServletResponse?
ResponseEntity can be used in a test environment
Successful status codes for:
Content-Type headerAccept headerWhen 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?
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?
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