https://github.com/amaanalikhan3000/springboot-fundamentals
https://github.com/amaanalikhan3000/springboot-fundamentals
java springboot-microservice springboot-starter
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/amaanalikhan3000/springboot-fundamentals
- Owner: amaanalikhan3000
- Created: 2024-09-12T17:22:19.000Z (9 months ago)
- Default Branch: dev
- Last Pushed: 2024-10-01T16:39:34.000Z (8 months ago)
- Last Synced: 2025-01-12T17:47:57.449Z (5 months ago)
- Topics: java, springboot-microservice, springboot-starter
- Language: Java
- Homepage:
- Size: 41 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Spring Boot API with `ResponseEntity`
This project demonstrates how to use `ResponseEntity` in a Spring Boot application to return various HTTP status codes based on different scenarios, such as `OK`, `Created`, `Bad Request`, `Not Found`, and `No Content`.
## How It Works
The `ResponseEntity` class in Spring Boot provides the flexibility to define both the response body and the HTTP status code. Below are common response scenarios implemented in this project:
### HTTP Response Scenarios
1. **404 Not Found**: When a requested resource is not available, the server returns `HttpStatus.NOT_FOUND`.
```java
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
```Example:
```bash
GET /api/entries/{id}
Response: 404 Not Found
```2. **201 Created**: When a new resource is created, the server returns `HttpStatus.CREATED` along with the created resource.
```java
return new ResponseEntity<>(myEntry, HttpStatus.CREATED);
```Example:
```bash
POST /api/entries
Response: 201 Created
```3. **400 Bad Request**: When invalid data is submitted, the server returns `HttpStatus.BAD_REQUEST`.
```java
return new ResponseEntity<>(myEntry, HttpStatus.BAD_REQUEST);
```Example:
```bash
POST /api/entries (invalid data)
Response: 400 Bad Request
```4. **200 OK**: When the requested resource is successfully retrieved, the server returns `HttpStatus.OK`.
```java
return new ResponseEntity<>(journalEntry.get(), HttpStatus.OK);
```Example:
```bash
GET /api/entries/{id}
Response: 200 OK
```5. **204 No Content**: When a resource is successfully deleted, the server returns `HttpStatus.NO_CONTENT`.
```java
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
```Example:
```bash
DELETE /api/entries/{id}
Response: 204 No Content
```