16

How can i consume json parameter in my webservice, I can able to get the parameters using @PathParam but to get the json data as parameter have no clue what to do.

@GET
@Path("/GetHrMsg/json_data")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_JSON)
public String gethrmessage(@PathParam("emp_id") String empid) {

}

What to use in place of @PathParam and how to parse it later.

Vivek Singh
  • 1,150
  • 2
  • 16
  • 39

3 Answers3

16

I assume that you are talking about consuming a JSON message body sent with the request.

If so, please note that while not forbidden outright, there is a general consensus that GET requests should not have request bodies. See the "HTTP GET with request body" question for explanations why.

I mention this only because your example shows a GET request. If you are doing a POST or PUT, keep on reading, but if you are really doing a GET request in your project, I recommend that you instead follow kondu's solution.


With that said, to consume a JSON or XML message body, include an (unannotated) method parameter that is itself a JAXB bean representing the message.

So, if your message body looks like this:

{"hello":"world","foo":"bar","count":123}

Then you will create a corresponding class that looks like this:

@XmlRootElement
public class RequestBody {
    @XmlElement String hello;
    @XmlElement String foo;
    @XmlElement Integer count;
}

And your service method would look like this:

@POST
@Path("/GetHrMsg/json_data")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public void gethrmessage(RequestBody requestBody) {
    System.out.println(requestBody.hello);
    System.out.println(requestBody.foo);
    System.out.println(requestBody.count);
}

Which would output:

world
bar
123

For more information about using the different kinds of HTTP data using JAXB, I'd recommend you check out the question "How to access parameters in a RESTful POST method", which has some fantastic info.

Community
  • 1
  • 1
bertag
  • 407
  • 3
  • 10
8

Bertag is right about the comment on the GET. But if you want to do POST request that consumes json data, then you can refer to the code below:

        @POST
        @Path("/GetHrMsg/json_data")
        @Consumes(MediaType.APPLICATION_JSON)
        public Response gethrmessage(InputStream incomingData) {
            StringBuilder crunchifyBuilder = new StringBuilder();
            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
                String line = null;
                while ((line = in.readLine()) != null) {
                    crunchifyBuilder.append(line);
                }
            } catch (Exception e) {
                System.out.println("Error Parsing: - ");
            }
            System.out.println("Data Received: " + crunchifyBuilder.toString());

            // return HTTP response 200 in case of success
            return Response.status(200).entity(crunchifyBuilder.toString()).build();
        }

For referencing please click here

Young Emil
  • 1,892
  • 1
  • 19
  • 30
  • 1
    I wish I would've scrolled down and seen this sooner. I spent a lot of time trying to tweak the marked answer to work for my POST request. This did the trick. Beautiful. – MuffinTheMan Apr 11 '19 at 17:13
4

@PathParam is used to match a part of the URL as a parameter. For example in an url of the form http:/example.com/books/{bookid}, you can use @PathParam("bookid") to get the id of a book to a method.

@QueryParam is used to access key/value pairs in the query string of the URL (the part after the ?). For example in the url http:/example.com?bookid=1, you can use @QueryParam("bookid") to get the value of `bookid.

Both these are used when the request url contains some info regarding the parameters and you can use the data directly in your methods.

Please specify the problem in detail if this post doesn't help you.

px06
  • 1,990
  • 1
  • 19
  • 40
kondu
  • 400
  • 2
  • 11