Spring Web Flashcards

1
Q

What does @RestController do?

A

Annotates a controller class with:
1. @Controller
2. @ResponseBody

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. What is a servlet?
  2. What is Spring MVC’s main servlet called?
A
  1. A servlet is a component of a servlet container used to handle HTTP requests and responses
  2. DispatcherServlet
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Spring MVC?

A

A Spring framework for building web applications

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Spring MVC

Describe the lifecycle of an HTTP request

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does @ResponseBody do?

A

Maps the return value of a controller method to an HTTP response body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does @RequestParam do?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does @PathVariable do?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why does Spring recommend using ResponseEntity instead of HttpServletResponse?

A

ResponseEntity can be used in a test environment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Successful status codes for:

  1. GET
  2. POST
  3. PUT
  4. PATCH
  5. DELETE
A
  1. 200
  2. 201
  3. 204
  4. 204
  5. 204
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. How does Spring MVC determine which message converter to use for an HTTP request?
  2. How does Spring MVC determine which message converter to use for an HTTP response?
A
  1. Content-Type header
  2. Accept header
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When should a “Location” header be sent back in an HTTP response?

A

Whenever an HTTP POST request is successful

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What class does Spring recommend for building URIs?

A

ServletUriComponentsBuilder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does @ExceptionHandler do?

A

It marks a controller method as an exception handler. For example:

  @ExceptionHandler(Exception.class)
  public ProblemDetail handleExceptions() {
    return ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR);
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

True or False

ResponseStatusException(…) can be thrown inside a controller method

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can a controller method declare a Pageable parameter?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does @RequestHeader do?

A

Maps HTTP request headers to a controller method parameter

17
Q

What are the 2 different ways to use @RequestMapping?

A
  1. To map an HTTP request to a controller class
  2. To map an HTTP request to a controller method
18
Q

What is the name of the Sring MVC starter?

A

spring-boot-starter-web

19
Q

How can you change the port of a Spring MVC application?

A

application.properties -> server.port = ?

20
Q

If you want to map HTTP HEAD, OPTIONS, and TRACE requests to a controller method, which annotation should you use?

A

@RequestMapping(method=?, path=?)

21
Q

Spring Boot

What bean should you use to instantiate a RestTemplate?

A

RestTemplateBuilder

22
Q

Does RestTemplate support streaming?

A

No. Consider using WebClient

23
Q

What are the 2 limitations of @ResponseStatus when used on a controller method?

A
  1. Overrides status codes that may have been defined inside the controller method
  2. Only works if the controller method does not throw an exception
24
Q

What is the main limitation of @ResponseStatus on a custom exception?

A

Returns an HTML error page making it unsuitable for RESTful applications. Consider using ResponseEntity instead

25
What is a servlet container?
A servlet container is software that runs inside a web server and is used to manage threads, filters, and servlets
26
Is it possible to map an HTTP request to a controller method by checking for specific headers, query parameters, or media types?
Yes. Look at @RequestMapping(...)
27
What is the difference between `@RequestBody` vs `ModelAttribute`?
`@RequestBody` maps the body of an HTTP request to a POJO and `@ModelAttribute` maps query parameters to a POJO
28
What is the difference between `@Valid` and `@Validated`?
`@Valid` enforces validation for all annotated fields. If any of the fields fail validation, the object is not created. `@Validated` enforces validation for either all annotated fields or specificed groups. This means it can be used for partial validation
29
If both Spring MVC and Spring WebFlux are on the classpath, which one will take precedence?
Spring MVC spring.main.web-application-type can be used to override this behavior
30
What is the purpose of `@ControllerAdvice`?
To create a class bean that handles exceptions thrown by controllers. Class methods are annotated with `@ExceptionHandler`. For example: ``` @RestControllerAdvice // or @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(HttpMessageConversionException.class) public ProblemDetail handleMessageConverterException(HttpMessageConversionException e) { return ProblemDetail.forStatus(HttpStatus.BAD_REQUEST); } @ExceptionHandler(Exception.class) public ProblemDetail handleExceptions() { return ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR); } ```
31
What 2 annotations makeup `@RestControllerAdvice`?
1. `@ControllerAdvice` (has `@Component`) 2. `@ResponseBody`