Я пытаюсь написать метод API PATCH, который удаляет один конкретный элемент из списка элементов.Обратите внимание, что список элементов является частью класса Menu.Там не так много ресурсов DropWizard, поэтому я немного застрял.
Вот все важные фрагменты кода - https://pastebin.com/Y9mAVZJk
Любая помощь будет много значить.Я начинающий, когда дело доходит до рестула apis, но я легко понял концепцию Angular.У меня проблемы с бэкэндом, особенно потому что это dropwizard.Это должно быть dropwizard, так как это задание, я не могу изменить его на что-либо еще.База данных может появиться позже, но это так, как сейчас.
public class Item {
private String person;
private String name;
private Integer quantity;
private Integer price;
public Item(String name, int price) {
this.person = "";
this.quantity = 0;
this.name = name;
this.price = price;
}
public Item(String person, String name, int quantity, int price) {
this.name = name;
this.person = person;
this.quantity = quantity;
this.price = price;
}
@JsonProperty
public String getPerson() {
return this.person;
}
@JsonProperty
public String getName(){
return this.name;
}
@JsonProperty
public Integer getQuantity(){
return this.quantity;
}
@JsonProperty
public Integer getPrice(){
return this.price;
}
}
____________________________
public class Menu {
private Integer id;
private String name;
private List<Item> items;
public Menu() { }
public Menu(int id, String name, List<Item> items) {
this.id = id;
this.name = name;
this.items = items;
}
@JsonProperty
public List<Item> getItems() {
return this.items;
}
@JsonProperty
public void setItems(List<Item> items){
this.items = items;
}
@JsonProperty
public Integer getId() {
return this.id;
}
@JsonProperty
public String getName() {
return this.name;
}
@JsonProperty
public void setId(final int id) {
this.id = id;
}
@JsonProperty
public void setName(final String name) {
this.name = name;
}
}
_____________________________
public MenuRepository() {
this.menus = new HashMap<>();
for (int i=1; i<9; i++) {
Item item = new Item("Item " + i, i+200);
this.items.add(item);
}
addNewMenu(new Menu(1, "First Menu", this.items));
addNewMenu(new Menu(2, "Second Menu", this.items));
addNewMenu(new Menu(3, "Third Menu", this.items));
addNewMenu(new Menu(4, "Fourth Menu", this.items));
addNewMenu(new Menu(5, "Fifth Menu", this.items));
addNewMenu(new Menu(6, "Sixth Menu", this.items));
addNewMenu(new Menu(7, "Seventh Menu", this.items));
}
private int getNewId() {
return counter++;
}
public Collection<Menu> getAllMenus() {
return this.menus.values();
}
public Menu get(final int id) {
return this.menus.get(id);
}
public Collection<Menu> addNewMenu(final Menu menu) {
menu.setId(this.getNewId());
this.menus.put(menu.getId(), menu);
return this.menus.values();
}
public Collection<Menu> removeMenu(final int id) {
this.menus.remove(id);
return this.menus.values();
}
@Path("/menu")
@Produces(MediaType.APPLICATION_JSON)
public class MenuResource {
private MenuRepository menuRepository;
public MenuResource(final MenuRepository menuRepository) {
this.menuRepository = menuRepository;
}
@GET
@Timed
public Collection<Menu> getAll() {
return this.menuRepository.getAllMenus();
}
@GET
@Path("/{id}")
@Timed
public Menu get(@PathParam("id") final int id) {
return this.menuRepository.get(id);
}
@POST
@Timed
public Collection<Menu> post(final Menu menu) {
return this.menuRepository.addNewMenu(menu);
}
@DELETE
@Path("/{id}")
@Timed
public Collection<Menu> delete(@PathParam("id") final int id) {
return this.menuRepository.removeMenu(id);
}
РЕДАКТИРОВАТЬ: Возможно ли получить метод PATCH для добавления и удаления элементов из списка?