2

I have a json object and i need topass it to my server via post.The json is hard coded inside my client. Below is my server program(test.java) - please note this is for learning purpose. So explain it clearly...

@Path("/json/product")
    public class Test {


        @POST
        @Path("/post")

        @Consumes(MediaType.APPLICATION_JSON)
        public Response createProductInJSON() {

            return Response.status(201).entity(....).build();



        }
      }

how json is gets passed from client to server?

I followed http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/ tutorial by mkyong(confused about post method). From a client program a hard coded json format needed to pass and recieved via post method...at server. Can the json pass from client to server as parameters...? or else how to do it.... below is my sample client program....

public static void main(String[] args) {

          try {

            URL url = new URL("http://localhost:8080/Snapshothealthapp1/rest/json/product/post");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            String JSON_DATA =
                     "{" 
                       + "  \"SnapshotRequest\": [" 
                       + "    {" 
                       + "      \"AuthenticationType\": \"email\"," 
                       + "      \"EmailAddress\": \"test@gmail.com\","                  
                       + "      \"Password\" : \"12345\"," 
                       + "      \"PracticeID\" : \"null\"," 
                       + "      \"DeviceID\" : \"null\""
                       + "    } +   ]"
                   + "}";
//          request.body("application/json", JSON_DATA);
//          String input = "{\"singer\":\"Metallica\",\"title\":\"Fade To Black\"}";

            OutputStream os = conn.getOutputStream();
            os.write(JSON_DATA.getBytes());
            os.flush();
            if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

            conn.disconnect();

          } catch (MalformedURLException e) {

            e.printStackTrace();

          } catch (IOException e) {

            e.printStackTrace();

         }

I need to pass the string json data to my server.Can anyone please give a client and server code for this?

Advance thanks

Plamen G
  • 4,729
  • 4
  • 33
  • 44
Miller
  • 744
  • 3
  • 15
  • 39

1 Answers1

3

Add @Consumes as "application/json" and specify the method parameter in JsonObject Type. You must have added javax.json.jar (normally comes with JAX-RS ) .

@Path("/create")
@POST
@Consumes (MediaType.APPLICATION_JSON) //Specify the POST MediaType as "application/json"
@Produces(MediaType.APPLICATION_JSON)
public Book createBook(JsonObject postData //Consumable JSON Object param  ) {

     System.out.println( postData); //This prints the client's JSON object from the Request Body

     //Do your work here with the postData ..........
     String value1 = postData.getString("key1");
     int value2 = postData.getInt("key2");
     //................
     //........
     //.....
}