2013-02-18 69 views

回答

6

Jersey提供了一个将JSON映射到Java对象的提供程序。要将请求主体映射到对象,只需将该对象指定为资源方法的参数即可。如果您需要原始JSON,请将对象指定为类型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) { 
     // .... 
    } 
} 
1
@POST 
public String handleRequest(String requestBody) { 
    logger.info(requestBody); 
    return "ok"; 
}