Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consume json parameter in java restful service

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.

like image 665
Vivek Singh Avatar asked Mar 11 '15 09:03

Vivek Singh


People also ask

How consume JSON from RESTful web service in spring boot?

Create a simple Spring Boot web application and write a controller class files which is used to redirects into the HTML file to consumes the RESTful web services. We need to add the Spring Boot starter Thymeleaf and Web dependency in our build configuration file. For Maven users, add the below dependencies in your pom.

How do I post JSON to a REST API endpoint in Java?

To post JSON to a REST API endpoint using Java, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the Java POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.

How do I return a JSON object to a REST web service?

Create RESTEasy Web Service to Produce JSON with @BadgerFish Now create a class whose methods will be exposed to the world as web service. Use JBoss @BadgerFish annotation that supports to return response as JSON. To return JSON as response we need to use media type as application/json.


2 Answers

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.

like image 156
bertag Avatar answered Oct 02 '22 10:10

bertag


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

like image 41
Young Emil Avatar answered Oct 02 '22 10:10

Young Emil