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