使用 PATCH 方法删除列表的特定元素,该元素是另一个列表的元素.使用 dropwizard 编写 [英] Removing a specific element of a list which is an element of another list using PATCH method. Written using dropwizard

查看:42
本文介绍了使用 PATCH 方法删除列表的特定元素,该元素是另一个列表的元素.使用 dropwizard 编写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to write a PATCH api method which removes one specific element from a list of items. Note that the list of items is part of the Menu class. There aren't a lot of dropwizard resources out there, so I'm kinda stuck.

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

Here's all the important pieces of code – https://pastebin.com/Y9mAVZJk

任何帮助都意义重大.我是restul apis的初学者,但我很容易掌握了Angular的概念.我遇到了后端问题,尤其是因为它是 dropwizard.它必须是 dropwizard,因为它是一项任务,我无法将其更改为其他任何内容.数据库可能会稍后出现,但它现在是这样.

Any help would mean a lot. I am a beginner when it comes to restul apis, but i've grasped the concept of Angular easily. I'm having issues with the backend, especially because it is dropwizard. It has to be dropwizard as it is an assignment, I can't change it to anything else. The databse might come later, but it is as it is right now.

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 方法从列表中添加和删除项目?

Is it possible to get the PATCH method to both add and remove items from the list?

推荐答案

我正在尝试在编写此答案时理解您的代码.

I'm trying to understand your code while writing this answer.

我想说,首先,尝试更好地格式化它,因为格式化的代码不仅对我们来说更容易阅读,对你来说也更容易阅读.

I'd say, first of all, try formatting it a little bit better, as formatted code is easier to read not only for us, but for you.

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

If I remember correctly, Dropwizard bundles Jersey, which is a JAX-RS implementation. In your resource class, just use the @PATCH annotation to identify the item removal method.

您可以获取 List 并通过 ListIterator 删除元素.

You can get a hold of the List<Item> and remove the element via 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);
   }

记得在Item类上添加@XmlRootElement注解

Remember to add the @XmlRootElement annotation on the Item class

@XmlRootElement
public class Item { ... }

<小时>

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


For the insertion/removal point, I'd say use two different methods.

您可以使用 add 作为插入路径的前缀,例如/add/{menuId} 并通过请求正文传递 Item.然后,您可以使用 remove 前缀删除路径,例如/remove/{menuId}/{itemId}

You can prefix the insertion path with add, e.g. /add/{menuId} and pass the Item via request body. You can then prefix the removal path with remove e.g. /remove/{menuId}/{itemId}

这篇关于使用 PATCH 方法删除列表的特定元素,该元素是另一个列表的元素.使用 dropwizard 编写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆