0

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.

1

3 Answers 3

5

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

3

Use native URLConnection or Apache HttpClient to send a HTTP request to the server.And the parameters must passed in key=value&key2=value2... format. So you may need to reconstruct the JSON object in that format or using another special parameter name like data=jsonstring then parse the json string using some library.

1
  • Thanks a lot.. can you also suggest me some java libraries to post as data=jsonstring in the request?
    – user915303
    Feb 7, 2012 at 7:26
2

@George has basically already answered your question, but in terms of JSON processing you may want to also look at Jackson http://jackson.codehaus.org/

This allows you to quickly convert Java objects to JSON equivalents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.