如何在泽西岛获得 JSON 正文?

2022-01-21 00:00:00 rest java jersey dropwizard

在泽西岛有 @RequestBody 等价物吗?

Is there a @RequestBody equivalent in Jersey?

@POST()
@Path("/{itemId}")
@Consumes(MediaType.APPLICATION_JSON)
public void addVote(@PathParam("itemId") Integer itemId, @RequestBody body) {
    voteDAO.create(new Vote(body));
}

我希望能够以某种方式获取已发布的 JSON.

I want to be able to fetch the POSTed JSON somehow.

推荐答案

你不需要任何注释.唯一没有注释的参数将是请求正文的容器:

You don't need any annotation. The only parameter without annotation will be a container for request body:

@POST()
@Path("/{itemId}")
@Consumes(MediaType.APPLICATION_JSON)
public void addVote(@PathParam("itemId") Integer itemId, String body) {
    voteDAO.create(new Vote(body));
}

或者你可以得到已经解析成对象的主体:

or you can get the body already parsed into object:

@POST()
@Path("/{itemId}")
@Consumes(MediaType.APPLICATION_JSON)
public void addVote(@PathParam("itemId") Integer itemId, Vote vote) {
    voteDAO.create(vote);
}

相关文章