3

I'm not talking about using the REST classes, like @Path or @POST.

I'm talking about how to translate any REST definition into a model of classes - A model from which many things can be done, like generating client code or parsing incoming data.

A bonus might be to parse an existing Resource class and create the model for it's REST API.

Example of such class might be Resource. A resource must have a path. A resource would have methods, like GET or POST. Methods would have params. And so on...

Methods can be different by definition, for example A POST method can have a stream of data, whereas GET cannot.

So, how to model a REST implementation using classes?

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
  • Because a REST API does not necessarily have a schema, is it possible to do it ? Unless you start from a manually written schema that is then converted to java classes. +1 because I am interested to know if there is standard way to do it... – Marco Altieri Feb 07 '16 at 16:48
  • 2
    It's not exactly what you are looking for, but [Swagger](http://swagger.io/) is an attempt for documenting and enabling easier implementations of REST clients. It does even have some code generators. – MJar Feb 07 '16 at 16:52
  • 1
    Althouh interesting question you should at least rephrase the last question (in bold) witch hunters here may find it off topic. – Jorge Campos Feb 07 '16 at 17:05
  • Jersey has such a model API it uses internally, _and_ to allow us to [build resources programmatically](https://jersey.java.net/documentation/latest/resource-builder.html), and more generally do any type of processing with the model. You can see example use [here](http://stackoverflow.com/a/34990765/2587435), and [here](http://stackoverflow.com/a/35237151/2587435) – Paul Samsotha Feb 07 '16 at 17:06
  • `Swagger` sounds exactly what I need. It seems the the class `Swagger` is the base of the model. Nice lock-in ;-) --- https://github.com/swagger-api/swagger-core/blob/master/modules/swagger-models/src/main/java/io/swagger/models/Swagger.java – AlikElzin-kilaka Feb 07 '16 at 17:06
  • Looking into `io.swagger.models.Path`, it seems more of a union data structure than proper class definition. Bummer :( – AlikElzin-kilaka Feb 07 '16 at 17:09
  • Often RESTful services have libraries which facilitate usage of the service. I've seen plenty of online and offline code for parsing JSON or XML files into POJO's. What exactly are you looking for? – K.Nicholas Feb 07 '16 at 18:40
  • @Nicholas - I'm talking about the Model. Parsing logic is just a bonus. – AlikElzin-kilaka Feb 08 '16 at 06:59
  • Generally people start with the model first, and create a rest interface from it. If you are looking at any given API from the outside, your either read their documentation and write the classes (i.e., **model** ) by hand, or you pass an example JSON or XML output to some program that creates POJO's (the **model**), both of which I have done. Then you point your unmarshaller to the package the classes live in and let rip. The `swagger`,project, now `open api initiative` seems to be trying to automate that process somewhat, but I didn't read it very closely. What is your question? – K.Nicholas Feb 08 '16 at 07:13

1 Answers1

1

You might find this post interesting : http://dalelane.co.uk/blog/?p=1871

It generates HTML file with an entry for each of a REST end-point in a specific path.

planben
  • 680
  • 6
  • 20