Hej,
Zaczynam zabawę z tworzeniem własnego API i mam pytanie odnośnie metody POST.
Ogólnie umiem stworzyć zapytanie i wszystko hula ALE tylko w przypadku gdy robie zapytanie "raw" tak jak poniżej:
{
"id": 10,
"name": "Test10",
"desc": "Desc10"
}
ale chciałbym też być w stanie tworzyć je w formacie form-data:
co powinienem dodac/zmienic żeby również w tej formie to działało ?
KOD Z API:
package API;
import model.Article;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/article")
public class ArticleAPI {
private List<Article> articleList;
public ArticleAPI(){
articleList = new ArrayList<>();
articleList.add(new Article(1, "Test1", "Desc1"));
articleList.add(new Article(2, "Test2", "Desc2"));
articleList.add(new Article(3, "Test3", "Desc3"));
}
@GetMapping("/all")
public List<Article> getAll(){
return articleList;
}
@GetMapping
public Article getById(@RequestParam int index){
Optional<Article> first = articleList.stream().filter(element -> element.getId() == index).findFirst();
return first.get();
}
@PostMapping()
public boolean addVideo(@RequestBody Article article){
return articleList.add(article);
}
@PutMapping
public boolean updateVideo(@RequestBody Article article){
return articleList.add(article);
}
@DeleteMapping
public boolean deleteVideo(@RequestParam int index){
return articleList.removeIf(element -> element.getId() == index);
}
}
package model;
public class Article {
private int id;
private String name;
private String desc;
public Article(int id, String name, String desc) {
this.id = id;
this.name = name;
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
- screenshot-20190901123312.png (15 KB) - ściągnięć: 64