How to set response header in REST API Spring Boot
What you learn here about Spring Boot
- How to set response header in REST API Spring Boot
How to set response header in REST API Spring Boot
Please follow the following steps to know to how to set response header in REST API Spring Boot
1)First please create a simple Maven project
2)Add the spring boot starter web dependency in your pom.xml which is shown below
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2)Now please create a restcontroller and create HttpHeaders and send in response using ResponseEntity which is shown below
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @org.springframework.web.bind.annotation.RestController public class RestController { @GetMapping("/users") public ResponseEntity<Object> getUser() { HttpHeaders headers = new HttpHeaders(); headers.add("transactionId", "ABC"); headers.add("version", "1"); headers.add("secret", "Xyp892PB"); return new ResponseEntity(headers, HttpStatus.OK); } }
3)Now please start your spring boot application
4)Now please open postman and make GET request for URL localhost:8080/users which is shown below