Hackforge Academy

Category: java

what is http status code

Published on 22 Apr 2026

Explanation

HTTP status codes are standard response codes returned by a server to indicate the result of a client's request such as success, error, or redirection.

Code:

// Example
200 OK
404 Not Found
500 Internal Server Error

Explanation

Common success status codes include 200 (OK), 201 (Created) 204 (No Content), which indicate the request was processed successfully.

Code:

@GetMapping("/success")
public ResponseEntity<String> success() {
  return ResponseEntity.ok("
Request successful");
}

Explanation

Use 201 status when a new resource is created, such as saving a user record in a database.

Code:

@PostMapping("/user")
public ResponseEntity<String> createUser() {
  return new ResponseEntity<>(
"User created", HttpStatus.CREATED
);
}

Explanation

Client error status codes like 400 (Bad Request) and 404 (Not Found) indicate problems with the request sent by the client.

Code:

@GetMapping("/user/{id}")
public ResponseEntity<String> getUser(
@PathVariable int id) {
  return new ResponseEntity<>(
"User not found", HttpStatus.NOT_FOUND);
}

Explanation

Server error status codes like 500 (Internal Server Error) indicate issues on the server side.

Code:

@GetMapping("/error")
public ResponseEntity<String> error() {
  return new ResponseEntity<>(
"Server error occurred", 
HttpStatus.INTERNAL_SERVER_ERROR
);
}

Explanation

You can also use @ResponseStatus annotation to directly set the HTTP status code for a controller method.

Code:

@ResponseStatus(HttpStatus.OK)
@GetMapping("/status")
public String getStatus() {
  return "Status set using annotation";
}

Explanation

ResponseEntity is the most flexible way to return HTTP status codes along with headers and body in Spring Boot REST APIs.

Code:

return ResponseEntity.status(
HttpStatus.BAD_REQUEST).body(
"Invalid input"
);

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community