SPRING MVC:

Todo API:

Create a Todo

Http Method: POST
Endpoint: /todo
Request Body:

{
    "name": "Make bed",
    "description": "Every morning at 7am",
}

Response:

201 Created
Body:

{
    "id" : "1", 
    "name": "Make bed",
    "description": "Every morning at 7am",
}

Get a Todo

Http Method: GET
Endpoint: /todo/{id}
Response:

200 OK
Body:

{
    "id" : "1", 
    "name": "Make bed",
    "description": "Every morning at 7am",
}

Spring Controllers

  • Annotate class with @RestController
    • This will register the class as a spring bean and as a controller in Spring MVC
  • To map methods to https paths use @RequestMapping
  • For a request body for a POST we use @RequestBody
  • For a path variable such as /todo/1, we use @PathVariable
  • @PostMapping (Create), @GetMapping (Read), @PutMapping (Update) @DeleteMapping (Delete) matches the Http Verbs (Methods)
@RestController
@RequestMapping("/todo")
public class TodoController {

    private final TodoService todoService;

    public TodoController(TodoService todoService) {
        this.todoService = todoService;
    }

    @PostMapping
    public ResponseEntity<TodoResponse> createTodo(@RequestBody TodoRequest request) {
        final Todo convertedTodo = convertRequestToTodo(request); 
        final Todo savedTodo = todoService.saveTodo(todo);
        return new ResponseEntity<>(savedTodo, HttpStatus.CREATED);
    }

    @GetMapping("{id}")
    public ResponseEntity<TodoResponse> getTodo(@PathVariable("id") Long id) {
        final Todo todo = todoService.findTodo(id); 
        return new ResponseEntity<>(todo, HttpStatus.OK));
    }

    @DeleteMapping("{id}")
    public ResponseEntity deleteTodo(@PathVariable("id") Long id) {
        todoService.delete(id);
        return new ResponseEntity<>(HttpStatus.OK));
    }
}

Jackson:

  • Jackson Marshalling: Serialise a Java Entity to a JSON String
  • Jackson Unmarshalling: Deserialise a JSON String into a Java Entity

Jackson annotations: @JsonCreator/@JsonProperty

  • @JsonCreator: Jackson has to know in what order to pass fields from a JSON object to the constructor. Because you have a single argument constructor, the creation works without @JsonCreator

  • @JsonProperty: Use it to rename the variable because the JSON is coming from a environment where properties start with an upper-case letter and hyphen. This maps to the java fields.

public class Parameter {
  @JsonProperty("Name")
  public String name;
  @JsonProperty("Last-Name")
  public String value; 
}

This correctly parses to/from the JSON:

"Parameter":{
  "Name":"Parameter-Name",
  "Last-Name":"Parameter-Value"
}

Todo Request/Response model:

public class TodoRequest {

    private final String name;

    private final String description;

    @JsonCreator
    public Todo(@JsonProperty("name") String name, 
                @JsonProperty("description") String description) {
        this.name = name;
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

}

public class TodoResponse {

    private final Long id; 

    private final String name;

    private final String description;

    public Todo(Long id, 
                String name, 
                String description) {
        this.name = name;
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

}

results matching ""

    No results matching ""