To run the project:
Step 1: CD to the project root directory cart_api
Step 2: Run ./mvnw spring-boot:run
command
Previously, we have
- Create a spring starter project with web dependency here
- Used several annotations to create RESTful API
@RestController
@RequestMapping
@RequestBody
@ResponseBody
@PathVariable
@Autowired
@Service
In this lesson, we will cover:
- Configure a custom singleton object using
@Bean
annotation - The use of
@Value
annotation to inject environmental variable - Implementing a logger to capture all API Input
Essentially, there are three common ways to configure and inject Spring Bean/Value.
- Using
@Component
or its children - @RestController, @Service, etcs.. - Using
@Value
to inject value picked up fromapplication.properties
file. - Using
@Bean
on a method that returns a configured object.
Practically, we will create this endpoint:
Path | Verb | Return Status | Remarks |
---|---|---|---|
/payment?payable=5.3 | POST | 200 | Payment successful |
400 | When payment is below minimum payable sum or negative value. |
- Generate a Spring Project with the web dependency added
- Annotations Explanations:
- @SpringBootApplication
- @RestController
- @RequestMapping
- @RequestBody
- @RequestResponse
By the end of the class, we would have created:
- GET /products
- GET /products/{id}
- POST /products
- PUT /products/{id}
- DELETE /products/{id}
Path | Verb | Has Request Body | Has Response Body | Purpose |
---|---|---|---|---|
/entities | GET | NO | YES | Get all entities (usually with pagination and sorting query parameters) |
/entities/{id} | GET | NO | YES | Get one entity by id |
/entities | POST | Yes | YES | Create an entity |
/entities/{id} | PUT | YES | YES | Update an entity |
/entities/{id} | DELETE | NO | NO | Delete an entity |
For more info - Link