|
| 1 | +package com.baeldung.responseheaders.controllers; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import javax.servlet.http.HttpServletResponse; |
| 8 | + |
| 9 | +import org.springframework.http.HttpHeaders; |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.web.bind.annotation.GetMapping; |
| 14 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 15 | +import org.springframework.web.bind.annotation.RestController; |
| 16 | + |
| 17 | +@RestController |
| 18 | +@RequestMapping("/single-response-header") |
| 19 | +public class ResponseHeaderController { |
| 20 | + |
| 21 | + @GetMapping("/http-servlet-response") |
| 22 | + public String usingHttpServletResponse(HttpServletResponse response) { |
| 23 | + response.addHeader("Baeldung-Example-Header", "Value-HttpServletResponse"); |
| 24 | + return "Response with header using HttpServletResponse"; |
| 25 | + } |
| 26 | + |
| 27 | + @GetMapping("/response-entity-constructor") |
| 28 | + public ResponseEntity<String> usingResponseEntityConstructor() { |
| 29 | + HttpHeaders responseHeaders = new HttpHeaders(); |
| 30 | + responseHeaders.set("Baeldung-Example-Header", "Value-ResponseEntityContructor"); |
| 31 | + String responseBody = "Response with header using ResponseEntity (constructor)"; |
| 32 | + HttpStatus responseStatus = HttpStatus.OK; |
| 33 | + |
| 34 | + return new ResponseEntity<String>(responseBody, responseHeaders, responseStatus); |
| 35 | + } |
| 36 | + |
| 37 | + @GetMapping("/response-entity-contructor-multiple-headers") |
| 38 | + public ResponseEntity<String> usingResponseEntityConstructorAndMultipleHeaders() { |
| 39 | + List<MediaType> acceptableMediaTypes = new ArrayList<>(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 40 | + HttpHeaders responseHeaders = new HttpHeaders(); |
| 41 | + responseHeaders.set("Baeldung-Example-Header", "Value-ResponseEntityConstructorAndHeaders"); |
| 42 | + responseHeaders.setAccept(acceptableMediaTypes); |
| 43 | + String responseBody = "Response with header using ResponseEntity (constructor)"; |
| 44 | + HttpStatus responseStatus = HttpStatus.OK; |
| 45 | + |
| 46 | + return new ResponseEntity<String>(responseBody, responseHeaders, responseStatus); |
| 47 | + } |
| 48 | + |
| 49 | + @GetMapping("/response-entity-builder") |
| 50 | + public ResponseEntity<String> usingResponseEntityBuilder() { |
| 51 | + String responseHeaderKey = "Baeldung-Example-Header"; |
| 52 | + String responseHeaderValue = "Value-ResponseEntityBuilder"; |
| 53 | + String responseBody = "Response with header using ResponseEntity (builder)"; |
| 54 | + |
| 55 | + return ResponseEntity.ok() |
| 56 | + .header(responseHeaderKey, responseHeaderValue) |
| 57 | + .body(responseBody); |
| 58 | + } |
| 59 | + |
| 60 | + @GetMapping("/response-entity-builder-with-http-headers") |
| 61 | + public ResponseEntity<String> usingResponseEntityBuilderAndHttpHeaders() { |
| 62 | + HttpHeaders responseHeaders = new HttpHeaders(); |
| 63 | + responseHeaders.set("Baeldung-Example-Header", "Value-ResponseEntityBuilderWithHttpHeaders"); |
| 64 | + String responseBody = "Response with header using ResponseEntity (builder)"; |
| 65 | + |
| 66 | + return ResponseEntity.ok() |
| 67 | + .headers(responseHeaders) |
| 68 | + .body(responseBody); |
| 69 | + } |
| 70 | +} |
0 commit comments