-1

I have created a rest webservice which has a below code in one method:

@POST
@Path("/validUser")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject validUserLogin(@QueryParam(value="userDetails") String userDetails){
    JSONObject json = null;
    try{
        System.out.println("Service running from validUserLogin :"+userDetails);
        json = new JSONObject(userDetails);
        System.err.println("UserName : "+json.getString("userName")+" password : "+json.getString("password"));
        json.put("httpStatus","OK");
        return json;            
    }
    catch(JSONException jsonException) {
       return json;
    }
}

I am using Apache API in the client code.And below client code is calling this service, by posting some user related data to this service:

public static String getUserAvailability(String userName){
    JSONObject json=new JSONObject();

    try{
        HttpContext  context = new BasicHttpContext();
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
        URI uri=new URIBuilder(BASE_URI+PATH_VALID_USER).build();
        HttpPost request = new HttpPost(uri);
        request.setHeader("Content-Type", "application/json");
        json.put("userName", userName);
        StringEntity stringEntity = new StringEntity(json.toString());
        request.setEntity(stringEntity);
        HttpResponse response = client.execute(request,context);
        System.err.println("content type : \n"+EntityUtils.toString(response.getEntity()));
    }catch(Exception exception){
        System.err.println("Client Exception: \n"+exception.getStackTrace());
    }
    return "OK";
}

The problem is, I am able to call the service, but the parameter I passed in the request to service results in null.

Am I posting the data in a wrong way in the request. Also I want to return some JSON data in the response, but I am not able to get this.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Vinay Pandey
  • 441
  • 3
  • 10
  • 22
  • So you are able to see that the request is received by the REST web service, but the userDetails string is null there? Are you sure the request makes it to the service? – Zack Feb 27 '15 at 21:36
  • Yes, I get the string printed out in the console as . "Service running from validUserLogin :null" – Vinay Pandey Feb 27 '15 at 21:39
  • In your client code, you put "userName" with `json.put("userName")`, did you mean to put `json.put("userDetails")` so it matches the parameter name in the REST service? Does that even matter in this case? – Zack Feb 27 '15 at 21:43
  • Yes, I tried that one as well, but the same error, I think i am not able post the data correctly – Vinay Pandey Feb 27 '15 at 21:46
  • Is there a way to send just the string, not as a JSON object, but as a string? There is a comment in this answer you could look at http://stackoverflow.com/a/10986030/1804496 it says "Pass just the value (i.e. not as a JSON object) and it should work, according to http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx " – Zack Feb 27 '15 at 21:51
  • No, Its not working here. the same result i am getting even with string, my service is not getting the parameter. There is some problem in the post request, that i am not able to figure out. – Vinay Pandey Feb 27 '15 at 22:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71918/discussion-between-user2039225-and-zack). – Vinay Pandey Feb 27 '15 at 22:11

1 Answers1

1

With the help of Zack , some how i was able to resolve the problem, I used jackson-core jar and changed the service code as below.

@POST
@Path("/validUser")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject validUserLogin(String userDetails){
  ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readValue(userDetails, JsonNode.class);

        System.out.println("Service running from validUserLogin :"+userDetails);

        System.out.println(node.get("userName").getTextValue());
        //node.("httpStatus","OK");
        return Response.ok(true).build();       

}
JNYRanger
  • 6,829
  • 12
  • 53
  • 81
Vinay Pandey
  • 441
  • 3
  • 10
  • 22
  • I changed the way i was returning the response: return Response.ok(true).entity("oK").build(); and it started working. – Vinay Pandey Feb 28 '15 at 07:35