How to get JSON body in jersey?

Is there an equivalent @RequestBodyin 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 somehow get POSTed JSON.

+4
source share
3 answers

You do not need an annotation. The only parameter without annotation will be the container for the 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 analyzed in the object:

@POST()
@Path("/{itemId}")
@Consumes(MediaType.APPLICATION_JSON)
public void addVote(@PathParam("itemId") Integer itemId, Vote vote) {
    voteDAO.create(vote);
}
+8
source
@javax.ws.rs.Consumes(javax.ws.rs.core.MediaType.APPLICATION_JSON) 

should already help you here and just so that the rest of the parameters are marked with annotations for which there were different types of parameters -

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

, json Vote, @RequestBody mathod, Spring Json Vote.

-2
source

Source: https://habr.com/ru/post/1663811/


All Articles