How to return HTTP status code in Spring Boot

What you will learn here about Spring boot

  • How to return HTTP status code in Spring Boot

How to return HTTP status code in Spring Boot

Please follow the following steps to know how to return HTTP status code in Spring Boot
1)First create a simple maven project

2)Then we have to add spring boot starter dependency in pom.xml which is shown below

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring boot starter web dependency

3)Please create a rest controller and use @ResponseEntity annotation to return body and HTTP status which is shown below.

@org.springframework.web.bind.annotation.RestController
public class RestController {
    @GetMapping("/hello")
    public ResponseEntity helloWorld(){

        return new ResponseEntity<>("Hello world", HttpStatus.OK);
    }
}

How to return HTTP status code in Spring Boot
4)Please run your Spring boot application

5)Now hit the url and check output. Below I have tested with postman which you can see below.
spring boot return status code

You may also like...