Restlet accepts JSON input from client and respond with POST
<p>I am writing a program which accepts a JSON input with the following format from client:</p>

<pre><code>{
    "campaignID": 1,
    "clientID": 1,
    "pmapID": 1,
    "ward": "1-Bedded (Private)",
    "age": 20,
    "attr1": "EXA1(A)",
    "attr2": "EO",
    "attr3": "11/02/2012",
    "attr4": "SIN",
    "attr5": "N",
    "attr6": "Y"
}
</code></pre>

<p>I'd like to read the JSON input, save all the attributes into local variables (String, int, ...) and finally respond with a POST("JSON") which will return a single float/double value (e.g. {"PMC": 30.12} ).</p>

<pre><code>public class RestletApplication extends Application
{
    @Override
    public synchronized Restlet createInboundRoot()
    {
        Router router = new Router(getContext());
        router.attach("/pmc/calculate", PMCResource.class);
        return router;
    }
}
</code></pre>

<p>I have written the function so far but am lost how to read the JSON input:</p>

<pre><code> public class PMCResource extends ServerResource
    {   
        @Post("JSON")
        public Representation post(Representation entity) throws ResourceException {
            try {
                if (entity.getMediaType().isCompatible(MediaType.APPLICATION_JSON))
                {
                    // Read JSON file and parse onto local variables

                    // Do processing &amp; return a float value

                }
            } catch (Exception e) {
                getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
            }
        }
    }
</code></pre>

<p><strong>5 May 2016</strong> - Edited the resource class</p>

<pre><code>// Imports

public class PMCResource extends ServerResource
{
    static Logger LOGGER = LoggerFactory.getLogger(PMCResource.class);

    @Override
    @Post("JSON")
    public Representation post(Representation entity) throws ResourceException
    {
        PMCMatrixDAO matrix = new PMCMatrixDAOImpl();
        JsonObjectBuilder response = Json.createObjectBuilder();

        try
        {
            if (entity.getMediaType().isCompatible(MediaType.APPLICATION_JSON))
            {
                InputStream is = new FileInputStream(getClass().getResource("/input.json").getFile());

                try (JsonReader reader = Json.createReader(is)) {
                    JsonObject obj = reader.readObject();
                    double result = matrix.calculatePMC(obj);
                    response.add("PMC", result);
                }
            }

        } catch (Exception e) {
            getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
        }

        return new StringRepresentation(response.build().toString());
    }
}
</code></pre>

<p>The Implementation class</p>

<pre><code>public class PMCMatrixDAOImpl implements PMCMatrixDAO
{       
    public double calculatePMC(JsonObject obj) 
    {   
        int campaignID = obj.getInt("campaignID");
        int clientID = obj.getInt("clientID");
        int pmapID = obj.getInt("pmapID");
        String ward = obj.getString("ward");
        int age = obj.getInt("age");
        String attr1 = obj.getString("attr1");
        String attr2 = obj.getString("attr2");
        String attr3 = obj.getString("attr3");
        String attr4 = obj.getString("attr4");
        String attr5 = obj.getString("attr5");
        String attr6 = obj.getString("attr6");

        // SQL processing
        double dPMC = sqlQueryCall(...);

        return dPMC;
    }
}
</code></pre>