简体   繁体   English

我如何在泽西岛获得JSON身体?

[英]How do I get the JSON body in Jersey?

Is there a @RequestBody equivalent in Jersey? 泽西岛有@RequestBody等价物吗?

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

I want to be able to fetch the POSTed JSON somehow. 我希望能够以某种方式获取POSTed JSON。

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);
}
@javax.ws.rs.Consumes(javax.ws.rs.core.MediaType.APPLICATION_JSON) 

should already help you here and just that the rest of the parameters must be marked using annotations for them being different types of params - 应该已经在这里帮助你了,只是必须使用注释来标记其余的参数,因为它们是不同类型的参数 -

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

如果你想让你的json作为投票对象,那么在你的mathod参数中简单地使用@RequestBody Vote body,Spring将自动转换你的投票对象中的Json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM