0
0
Spring Bootframework~5 mins

Custom response headers in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom response header in Spring Boot?
A custom response header is an additional piece of information added to the HTTP response by the server. In Spring Boot, you can add these headers to provide extra data to the client beyond the standard headers.
Click to reveal answer
beginner
How do you add a custom response header using Spring Boot's ResponseEntity?
You can add custom headers by creating an HttpHeaders object, adding your headers to it, and then passing it to ResponseEntity. For example: <br>HttpHeaders headers = new HttpHeaders();<br>headers.add("X-Custom-Header", "value");<br>return new ResponseEntity<>(body, headers, HttpStatus.OK);
Click to reveal answer
intermediate
What is the purpose of the @ResponseHeader annotation in Spring Boot?
Spring Boot does not have a @ResponseHeader annotation. Instead, you add custom headers using HttpServletResponse, ResponseEntity, or WebFilter. This is important to know to avoid confusion.
Click to reveal answer
beginner
How can you add a custom header using HttpServletResponse in a Spring Boot controller?
You can inject HttpServletResponse into your controller method and call response.addHeader("X-Custom-Header", "value") before returning the response body.
Click to reveal answer
beginner
Why might you want to add custom response headers in a web application?
Custom headers can provide extra information like security tokens, version info, or debugging data. They help clients understand or handle the response better without changing the response body.
Click to reveal answer
Which class in Spring Boot is commonly used to add custom response headers?
AHttpServletRequest
BHttpHeaders
CRequestBody
DPathVariable
How do you add a custom header using HttpServletResponse?
Aresponse.addHeader("X-Name", "value")
Bresponse.setBody("X-Name", "value")
Cresponse.getHeader("X-Name")
Dresponse.removeHeader("X-Name")
What is the correct way to return a response with custom headers using ResponseEntity?
Anew ResponseEntity<>(headers, body, status)
Bnew ResponseEntity<>(status, headers, body)
Cnew ResponseEntity<>(body, headers, status)
Dnew ResponseEntity<>(body, status, headers)
Which of these is NOT a reason to add custom response headers?
ATo change the HTTP method
BTo send extra info like tokens
CTo provide version info
DTo help with debugging
Can you add multiple custom headers in Spring Boot response?
AOnly if you use @ResponseHeader
BNo, only one header is allowed
COnly with XML configuration
DYes, by adding multiple headers to HttpHeaders
Explain how to add a custom response header in a Spring Boot controller using ResponseEntity.
Think about how you build the response with headers included.
You got /3 concepts.
    Describe why custom response headers are useful in web applications.
    Consider what extra data headers can carry.
    You got /4 concepts.