在 Rest web 方法中传递 JSON 对象 [英] Pass JSON Object in Rest web method
问题描述
@GET
@Path("/addemployee")
@Produces(MediaType.APPLICATION_JSON)
public void addEmployee(@QueryParam("id") String id, @QueryParam("name") String name,@QueryParam("address") String address,@QueryParam("phone") String phone ) {
employeeVo.setId(Integer.parseInt(id));
employeeVo.setName(name);
employeeVo.setPhone(phone);
employeeVo.setAddress(address);
employeeDao.addNewEmployee(employeeVo);
}
我有上述方法,它从 html 表单中获取参数并将此数据添加到数据库表中.我想做同样的事情,但使用 json 对象,那么如何将 Json 对象作为参数传递呢?以及我应该向我的 Pom.xml 文件添加什么依赖项.提前致谢
I have this above method which takes parameter from an html form and add this data to database table. I want to do the same but using json Object, So how to pass a Json Object as a parameter? and what dependency should I add to my Pom.xml file. Thanks In advance
推荐答案
假设您使用 JAX-RS
实现,您在 发布
数据>JSON 在您的请求正文中表示您的 POJO
EmployeeVo,您可以简单地这样做:
Assuming that you use a JAX-RS
implementation, that you post
your data following the JSON
representation of your POJO
EmployeeVo in the body of your request, you can simply do that:
@POST
@Path("/addemployee")
@Consumes(MediaType.APPLICATION_JSON)
public void addEmployee(EmployeeVo employeeVo) {
employeeDao.addNewEmployee(employeeVo);
}
以下是在请求正文中发布的相应 JSON 对象的示例:
Here is an example of the corresponding JSON object to post in the body of your request:
{
"id" : 1,
"name" : "foo",
"phone" : "911",
"address" : "bar",
}
这篇关于在 Rest web 方法中传递 JSON 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!