I have a web service that returns JSON strings one by one based on query inputs, a GET request to the service returns this (there's only one entry in the database)
[{"checked":false,"dateposted":"2014-10-23T00:00:00","itemnumber":1,"quantity":5,"stockcheckid":1}]
at the moment I just have this System.out.println
in a while loop.
what I want to do is be able to access these results in a way that I can input them into a jtable to display on a client application. I've read some guides about reading from JSON files etc but I cant find anything specific to REST web services. I hear GSON mentioned a lot, i tried that but i cant quite work out how to make it work in this context
I should also mention that the service can also send this data in XML format.
Do I somehow create a JSON file appending each new entry to it? and then populate the table from that file?
anyway here's the code that initiates the GET request.
public static void getRequest(String dataGet) {
try {
URL url = new URL("http://localhost:8080/nXXXXXXXXc/webresources/entities.stockchecks/" + dataGet);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}`
Irrespective of how you obtain the data, use it to construct a suitable
TableModel
and use that model to construct theJTable
. In this example, the model accesses aMap<String, String>
to fulfill theTableModel
contract; you can substitute theMap
obtained using the approach shown here. Because loading the data may take an indeterminate amount of time, use aSwingWorker
, as shown here.This is simply a combination of trashgod's and tom's answer, with an example, using Jackson and a TableModel. I really just want to give camickr's BeanTableModel/RowTableModel (which is a generic class to help us easily create table models to from pojos) a try (seems to work great).
For more information/details please see this post
Entity
class (properties mapped to the keys in your json)Main class. Note the use
BeanTableModel
. You will need to download this class along withRowTableModel
from the link above.Result
Note, for long running task (many io task included), such as requesting a rest resource, you should use a
SwingWorker
as explained by trashgod. When the repsonse comes in, you can basicallyaddRow
to theRowTableModel
instance. For instance if we use the same json response and model as above, we could simply do something likeUPDATE
Looking at your json, it is an array of objects. With XML, the format is a little different, as you must have a root document element. So you can't just have say
It would need to be something like
That being said, using data binding, the easiest way would be to create another class to wrap a
List<Entity>
. Now I'm not too familiar with Jackson's XML capabilities/features, but using JAXB, you can have a class like:Then you can unmarshal the below XMl into the
Entities
class. Here's an update demo to show both json and xmlThe response is a map. You can serialise a JSON map to a Java map with Jackson like this:
I imagine you could do something similar with GSON. Another option is if you know the structure of the JSON object - in that case you make a simple POJO version and de-serialise into that instead of something like the POJO class I've defined above.
More detail and a similar version that supports XML as well as JSON mapping