Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get raw POST with Jersey?

Tags:

post

jersey

How can I get the raw POST with Jersey? @FormParam won't work because I'm posting raw JSON not in any specific POST field.

like image 489
Webnet Avatar asked Feb 18 '13 16:02

Webnet


2 Answers

Jersey comes with a provider for mapping JSON to Java objects. To map your request body to an object simply specify that object as an argument to your resource method. If you want the raw JSON, specify the object to be of type java.lang.String.

@Path("/mypath")
public class MyResource {

    /**
     * @param pojo Incoming request data will be deserialized into this object
     */
    @POST
    @Path("/aspojo")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response myResourceMethod(MyPojo pojo) {
        // ....
    }

    /**
     * @param json Incoming request data will be deserialized directly into
     *    this string
     */
    @POST
    @Path("/asjson")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response myResourceMethod(String json) {
        // ....
    }
}
like image 187
Perception Avatar answered Sep 30 '22 17:09

Perception


@POST
public String handleRequest(String requestBody) {
    logger.info(requestBody);
    return "ok";
}
like image 36
m.kocikowski Avatar answered Sep 30 '22 15:09

m.kocikowski