使用PATCH方法删除列表的特定元素,该元素是另一个列表的元素。使用dropwizard编写

时间:2019-02-13 20:32:39

标签: java angular rest api dropwizard

我正在尝试编写PATCH api方法,该方法从项目列表中删除一个特定元素。请注意,项目列表是Menu类的一部分。那里没有很多dropwizard资源,所以我有点卡住了。

这是所有重要的代码– https://pastebin.com/Y9mAVZJk

任何帮助都将有很多意义。我是Restul API的初学者,但是我很容易理解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方法在列表中添加和删除项目?

1 个答案:

答案 0 :(得分:0)

在编写此答案时,我试图理解您的代码。

我想说的是,首先,请尝试对它进行更好的格式化,因为格式化后的代码不仅对我们而且对您也更容易阅读。

如果我没记错的话,Jersey捆绑了JAX-RS,这是一个@PATCH的实现。在资源类中,只需使用List<Item>批注来标识项目删除方法。

您可以保留ListIterator<Item>,并通过@Path("/menu") @Produces(MediaType.APPLICATION_JSON) public class MenuResource { ... @PATCH @Path("/remove/{menuId}/{itemName}") public void removeMenuItem( @PathParam("menuId") final int menuId, @PathParam("itemName") final String itemName) { final List<Item> items = this.menuRepository.get(menuId).getItems(); for (final ListIterator<Item> iterator = items.listIterator(); iterator.hasNext();) { final Item item = iterator.next(); if (itemName.equals(item.getName())) { iterator.remove(); break; } } } @PATCH @Path("/add/{menuId}") public void removeMenuItem( @PathParam("menuId") final int menuId, final Item item) { // Item represents the Request Body final List<Item> items = this.menuRepository.get(menuId).getItems(); items.add(item); } 删除该元素。

@XmlRootElement

请记住在Item类上添加@XmlRootElement public class Item { ... } 批注

add

对于插入/移除点,我想说使用两种不同的方法。

您可以在插入路径前加上/add/{menuId},例如Item并通过请求正文传递remove。 然后,您可以在删除路径前加上/remove/{menuId}/{itemId},例如{{1}}