Pritesh Naik

Pritesh Naik

  • NA
  • 16
  • 794

java rest web service using post

Sep 23 2017 7:38 AM
I am trying to do a java rest web service using "POST" method.My client part to invoke the web service is working proper.But i am facing difficulty in accessing the passed parameters by "POST" method.Any help would be appreciable. Here is my client side.
 
   
 public static void main(String[] args) throws IOException { String urlParameters = "param1=world&param2=abc"; String request = "http://localhost:8080/wsRevDash/rest/post/test";      URL url = new URL(request); HttpURLConnection conn = (HttpURLConnection) url.openConnection();     conn.setDoOutput(true);     conn.setInstanceFollowRedirects(false);     conn.setRequestMethod("POST");      conn.setRequestProperty("charset", "utf-8"); DataOutputStream wr = new DataOutputStream(conn.getOutputStream());     wr.writeBytes(urlParameters);     wr.flush();     wr.close(); Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); for (int c; (c = in.read()) >= 0;) System.out.print((char)c); }

And here is my java rest web service
 @POST @Path("/test") @Produces(MediaType.APPLICATION_JSON) public String getpostdata(@QueryParam("param1") String param1,@QueryParam("param2") String param2) { JSONObject jObjDevice = new JSONObject();           jObjDevice.put("Hello",param1); return jObjDevice.toJSONString(); }

The parameter param1=world.But when i run the web service,I am getting json string as {"Hello":null} instead of {"Hello":"world"}.Please suggest for any changes.I think the problem is with annotation i.e @QueryParam.If this is the case,then please provide proper way to access the parameter using proper annotation.

Answers (1)