blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

JAX-RS: Sending/Receiving JSON data

Server code,
@Path("/hello")
public class HelloResource {
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject helloPost(JSONObject jsonRequest) {
        JSONObject jsonResponse = new JSONObject();
        jsonResponse.put("ab",  jsonRequest.get("a").toString()+jsonRequest.get("b"));
     return jsonResponse;

    }
}

Client code,
String lsUrl = "http://localhost:9080/RSServer/HelloApp/hello";
RestClient client = new RestClient();

Resource resource = client.resource(lsUrl);

JSONObject json = new JSONObject();

json.put("a", "hello");
json.put("b", " world");

System.out.println("JSON input: "+json);

String result = (String) resource.contentType(MediaType.APPLICATION_JSON).post(String.class, json);
System.out.println(result);
{"ab":"hello world"}


No comments:

Post a Comment