no

Spring Boot RESTful API - CRUD Annotations

Go Back to Course Outline Repositories https://github.com/terawarehouse/terawarehouse-catalog https://github.com/terawarehouse/tera...


Go Back to Course Outline

Repositories
We will now dissect the CategoryController class, which is annotated to expose a set of REST endpoints.

[open CategoryController.java]
@RestController - It’s basically just a @Controller with @ResponseBody. It is used to annotate the controller class to indicate that it supports REST. It comes with @RequestMapping to define class level REST properties such as the path and what media type is produced.

For swagger documentation we use @ApiOperation, @ApiParam, and @ApiResponses.

Here are the most commonly used REST annotations in reference to CRUD.
  • Create / Http Post / @PostMapping - Creates a new entity. It returns the entity with the auto-generated id.
  • Update / Http Put / @PutMapping - Updates an existing entity. It also returns the updated entity.
  • Find/List / Http Get / @GetMapping - Returns an entity.
  • Delete / Http Delete / @DeleteMapping - Deletes an entity
  • Patch / Http Patch / @PatchMapping - In general Put should send all the fields of an entity during an update while patch just sends a subset. But in general, we just use Put.
These are mostly the core annotations that you will use again and again. But it should not be a problem as it’s generally easy to get familiar with them.

To test endpoints, I have preferred a Postman collection which is available in src/test/resources.

Let’s do some testing!

[open Postman collection]

Run the tests manually one by one. Change the uid parameter by getting the uid returned from the newly created category.

Next, we will discuss how we can automate request validation.

Related

coding 1417498228035025285

Post a Comment Default Comments

item