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));
}
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);
}
@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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With