How to get the http request body in the API

Asked

Viewed 848 times

0

How do I access the body content of the request sent by the client? For example:

@Override
@GET
@Path("/mypath")
public Response myMethod() {
    System.out.println("Conteúdo da requisição: " + request.body);

    return null;
}
  • Try to put @Context HttpServletRequest request as a parameter of the method.

  • I even tried, as I have seen in several similar problems. Only the eclipse cannot see the references and does not recognize the Class. In the end, it always gives java.lang.ClassNotFoundException: HttpServletRequest

  • Even looking for the class (Ctrl + T) can’t find?

  • Unfortunately, no :(

  • The eclipse is not happening import of no lib that comes from javax.servlet.*. For some reason, you don’t recognize.

  • Can you add dependencies to the project? Try inserting that one.

  • The right thing would be that one in fact. But there you see your need.

  • The import worked with the second reference, but I followed some links on Soen - here, for example - I didn’t get any return.

  • request is loaded when it enters the method?

  • @Gustavocinque, I was able to solve the problem by adding your lib suggestion to what is cited in that reply

  • 1

    Cool, @Hamurabiaraujo. Even though I don’t understand how to check if the method used is POST or would not help you. But even so, a tip, class javax.ws.rs.HttpMethod keeps enums for the most common methods (POST, GET, PUT, DELETE, HEAD and OPTIONS). It is worth using and not having to make the comparison with raw string.

  • It is because it really does not make sense to exist this verification. Now that you spoke, I went to analyze right (rsrs). Because the method only accepts request @POST, then there is no need for such a test within the method itself.

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.