Gradle Project
Java
2.1.8
com.example
todoapp
Spring Web
Spring Data JPA
H2 Database
Lombok
com.example.todoapp
controllers
repositories
models
services
, mais cette fois, il est omismodels
package com.example.todoapp.models;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Data
@Entity
public class Task {
@Id
@GeneratedValue
private Long id;
private String summary;
private Boolean done = false;
public Task(){}
}
@Data
toString ()
et la méthode hashCode ()
@Entity
@Id
@GeneratedValue
stratégie
ou générateur
(je ne l'ai pas essayé)repositories
package com.example.todoapp.repositories;
import com.example.todoapp.models.Task;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TaskRepository extends JpaRepository<Task, Long> {
}
JpaRepository
CrudRepository
dans l'ancêtre de JpaRepository
définit une interface telle que findById
JpaRepository
spécifie la classe d'entité cible et son type de clé primairefindById
et deleteById
.Task
créée cette fois-ci est de type Long
, spécifiez Long
ici.controllers
package com.example.todoapp.controllers;
import com.example.todoapp.models.Task;
import com.example.todoapp.repositories.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping(value = "/api")
public class ApiController {
@Autowired
TaskRepository taskrepo;
@GetMapping(value = "/todos")
public List<Task> tasklist(){
return taskrepo.findAll();
}
@GetMapping(value = "todo/{id}")
public Optional<Task> retrieve(@PathVariable Long id){
return taskrepo.findById(id);
}
@PostMapping(value = "/todo")
public ResponseEntity<Task> newTask(@RequestBody Task task){
Task result = taskrepo.save(task);
return new ResponseEntity<Task> (result, HttpStatus.CREATED);
}
@PutMapping(value = "/todo/{id}")
public Task update(@PathVariable Long id, @RequestBody Task task){
task.setId(id);
return taskrepo.save(task);
}
@DeleteMapping(value = "/todo/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id){
taskrepo.deleteById(id);
}
}
@RestController
@ Controller
, il semble que la valeur de retour de la méthode soit transformée en String et intégrée au modèle.
@RequestMapping
@Autowired
@ Component
@ Repository
et @ Service
ont également @ Component
ajouté en interne.TaskRepository
sont automatiquement stockées dans taskrepo
.
@GetMapping
, @PostMapping
, @PutMapping
, @DeleteMapping
@ RequestMapping
@ GetMapping
, cela équivaut à @RequestMapping (method = RequestMethod.GET)
@ResponseStatus
$ curl http://localhost:8080/api/todo -XPOST -H 'Content-Type: application/json' -d '{"summary": "my first task"}' | jq .
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 75 0 47 100 28 170 101 --:--:-- --:--:-- --:--:-- 170
{
"id": 1,
"summary": "my first task",
"done": false
}
$ curl http://localhost:8080/api/todos | jq .
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 49 0 49 0 0 285 0 --:--:-- --:--:-- --:--:-- 286
[
{
"id": 1,
"summary": "my first task",
"done": false
}
]
$ curl http://localhost:8080/api/todo/1 | jq .
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 47 0 47 0 0 2045 0 --:--:-- --:--:-- --:--:-- 2136
{
"id": 1,
"summary": "my first task",
"done": false
}
$ curl http://localhost:8080/api/todo/1 -XPUT -H 'Content-Type: application/json' -d '{"summary": "updated my task", "done": true}' | jq .
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 92 0 48 100 44 2632 2412 --:--:-- --:--:-- --:--:-- 2666
{
"id": 1,
"summary": "updated my task",
"done": true
}
$ curl http://localhost:8080/api/todo/1 -XDELETE -v
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> DELETE /api/todo/1 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 204
< Date: Wed, 25 Sep 2019 15:29:37 GMT
<
* Connection #0 to host localhost left intact
Recommended Posts