Use Jersey to develop Restful interface in Dropwizard (modify, delete)

Earlier we introduced the use of Jersey to develop queries, add new interfaces, and standardize the response message format. If you want to know more, you can refer to:

Use Jersey to develop Restful interface (query interface) in Dropwizard icon-default.png?t=N4P3https://blog.csdn.net/m1729339749/article/details/130812817

Use Jersey to develop Restful interface in Dropwizard (response message format, new addition) icon-default.png?t=N4P3https://blog.csdn.net/m1729339749/article/details/130923232

In this article, we introduce the interfaces for development, modification and deletion using Jersey:

1. Modify the product

@PUT
@Path("/{id}")
public Response update(@PathParam("id") String id, Goods goods) {
    // 查询商品
    Goods hadGoods = findById(id);
    
    // 判断商品是否存在
    if(Objects.isNull(hadGoods)) {
        return Response.ok().entity(new ResponseResult(404, "商品不存在")).build();
    }

    // 修改商品名称
    hadGoods.setName(goods.getName());
    return Response.ok().entity(new ResponseResult(0, "成功", hadGoods)).build();
}

@PUT : Represents the request method, the request method using PUT

@PathParam : Represents obtaining parameters from the request path

The code checks whether the product exists. After the verification is successful, the name of the product is modified.

Test: modify the product

Request method: PUT

Request address: http://localhost:8080/goods/2

Request message:

{ "name": "火腿肠" }

The response results are as follows:

{
    "code": 0,
    "msg": "成功",
    "data": {
        "id": "2",
        "name": "火腿肠"
    }
}

The results of the product query are as follows:

{
    "code": 0,
    "msg": "成功",
    "data": [
        {
            "id": "1",
            "name": "薯片"
        },
        {
            "id": "2",
            "name": "火腿肠"
        }
    ]
}

2. Delete the product

@DELETE
@Path("/{id}")
public Response delete(@PathParam("id") String id) {
    for(Iterator<Goods> it = goodsList.iterator(); it.hasNext(); ) {
        // 获取商品
        Goods goods = it.next();
        // 判断商品编号是否一致
        if(goods.getId().equals(id)) {
            // 移除商品
            it.remove();
            return Response.ok().entity(new ResponseResult(0, "成功", goods)).build();
        }
    }

    // 未找到商品
    return Response.ok().entity(new ResponseResult(404, "商品不存在")).build();
}

@DELETE : Represents the request method, the request method using DELETE

@PathParam : Represents obtaining parameters from the request path

Test: delete item

Request method: DELETE

Request address: http://localhost:8080/goods/2

The response results are as follows:

{
    "code": 0,
    "msg": "成功",
    "data": {
        "id": "2",
        "name": "可口可乐"
    }
}

The results of the product query are as follows:

{
    "code": 0,
    "msg": "成功",
    "data": [
        {
            "id": "1",
            "name": "薯片"
        }
    ]
}

Guess you like

Origin blog.csdn.net/m1729339749/article/details/130925007