FAQ: RESTful web service with Jersey&Jackson

  • How to do output in JSON format
    Server: 

    @POST
    @Path("{id}")
    @Produces("application/json")  
    @Consumes("application/json")  
    public JSONObject sayPlainTextHello(@PathParam("id")JSONObject inputJsonObj) {  
    String input = (String) inputJsonObj.get("input");  
    String output="The input you sent is :"+input;  
    JSONObject outputJsonObj = new JSONObject();  
    outputJsonObj.put("output", output);  
    return outputJsonObj;  
    }

    Client:

    ClientConfig config = new DefaultClientConfig();  
    Client client = Client.create(config);  
    WebResource service = client.resource(getBaseURI());  
    JSONObject inputJsonObj = new JSONObject();  
    inputJsonObj.put("input", "Value"); System.out.println(service.path("rest").path("hello").accept(MediaType.APPLICATION_JSON).entity(inputJsonObj).post(JSONObject.class,JSONObject.class));
    
This entry was posted in Java. Bookmark the permalink.