且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过客户端的post方法使用json对象

更新时间:2021-10-07 22:03:58

将@Consumes添加为"application/json",并在JsonObject Type中指定method参数.您必须添加了javax.json.jar(通常随 JAX-RS ).

Add @Consumes as "application/json" and specify the method parameter in JsonObject Type. You must have added javax.json.jar (normally comes with JAX-RS ) .

@Path("/create")
@POST
@Consumes (MediaType.APPLICATION_JSON) //Specify the POST MediaType as "application/json"
@Produces(MediaType.APPLICATION_JSON)
public Book createBook(JsonObject postData //Consumable JSON Object param  ) {

     System.out.println( postData); //This prints the client's JSON object from the Request Body

     //Do your work here with the postData ..........
     String value1 = postData.getString("key1");
     int value2 = postData.getInt("key2");
     //................
     //........
     //.....
}