mercredi 27 septembre 2017

java rest web service by post method

I am trying to do a java rest web service using "POST" method.But i am unable to access the passed parameter. Here is my client part to invoke the web service.

    public static void main(String[] args) {

        try {

            Client client = Client.create();

            WebResource webResource = client.resource("http://localhost:8080/wsRevDash/rest/post/testing");


            Form form=new Form();

            form.add("sc","pqr");



            ClientResponse response = webResource.type("application/json")
               .post(ClientResponse.class,form);

            if (response.getStatus() != 201) {
                throw new RuntimeException("Failed : HTTP error code : "
                     + response.getStatus());
            }

            System.out.println("Output from Server .... \n");
            String output = response.getEntity(String.class);
            System.out.println(output);

          } catch (Exception e) {

            e.printStackTrace();

          }

        }

And here is my java rest web service.

    @POST
    @Path("testing")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String createDataInJSON(@FormParam("sc") String data) { 

        System.out.println("Hello"+data);
        JSONObject jObjDevice = new JSONObject();


        jObjDevice.put("Hello",data); 
        return jObjDevice.toJSONString();

    }

When i run on SoapUI,I am getting {"Hello":null}. Please suggest me some way to cope with this.




Aucun commentaire:

Enregistrer un commentaire