javaweb-servicesrest

How to make a REST Webservice call in JAVA?


Currently I have a web service running in a tomcat (http://localhost:8080/myApp/getUsers). My web service will accept a json string and then process accordingly. My webservice code is as follows:

@Path("/getUsers")
public class UsersWS
{
    @POST
    public Response post(String theRequestJSON)
    {
        try
        {
            JSONObject aJsonObj = new JSONObject(theRequestJSON);
            String userID = aJsonObj.getString("userID");   
            System.out.println(userID);
        }
    }
}

So, my Web service is processing a json string. So now, I need to call the above web service from another JAVA class (with a jsonObject having the userID in request parameter).

How to do it? Shortly, I need to make a web service call from a JAVA class with a JSON object as a request parameter. How to send a json as a request parameter in a request call.


Solution

  • Take a look at Jersey: http://jersey.java.net

    Here's a good write up on how to use the client: http://blogs.oracle.com/enterprisetechtips/entry/consuming_restful_web_services_with