1
@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);     
}

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

os.Kh
  • 45
  • 1
  • 10

2 Answers2

0

Why don't you just accept the JSON as String in the service and make a JSON object out of it.
You can make use of the dependency below for making the JSON object.

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>
java_maniac
  • 101
  • 7
0

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);     
}

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",
 }
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • Thanks for the reply, I'm confused about how to send the json Object in the URL ? – os.Kh Apr 18 '16 at 14:14
  • Assuming that you have curl, have a look to this question http://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest – Nicolas Filotto Apr 18 '16 at 14:23
  • what is import in this post is _curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login_ – Nicolas Filotto Apr 18 '16 at 14:24
  • Sorry but I don't know what is curl .. I just want to pass data in a simple way as Json object.. is there another way to do that ? – os.Kh Apr 18 '16 at 14:27
  • curl is simply a little application allowing to launch HTTP request from a terminal, which os do you use? – Nicolas Filotto Apr 18 '16 at 14:28
  • windows ... and building web service using eclipse – os.Kh Apr 18 '16 at 14:29
  • If you don't want to use curl, you can use JQuery http://www.w3schools.com/jquery/ajax_post.asp – Nicolas Filotto Apr 18 '16 at 14:30
  • currently I just want to do this using get method from the url as I'm doing in the above code.. but now i want to do this using json object instead of QueryParam .. – os.Kh Apr 18 '16 at 14:33
  • it is not your role to manipulate a JSON object when you use a JAX RS implementation, you are supposed to deal with POJO, it is the role of your JAX-RS implementation to convert your JSON data into a POJO. But If you really want to use the get method, you can either implement your own Provider or simply send the String representation of the JSON object through a given parameter then parses it manually but it is not what you are supposed to do once again – Nicolas Filotto Apr 18 '16 at 14:44
  • BTW which JAX-RS impl do you use? – Nicolas Filotto Apr 18 '16 at 14:44
  • I guess jboss ? actually I'm new to web service and I'm not sure.. thanks for the explanation you helped me a lot :) – os.Kh Apr 18 '16 at 14:51