public static void main(String[] args) throws IOException { String urlParameters = "param1=world¶m2=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.