"Have you ever bored yourself by creating repeated webservices resource code? I have! That's why I wrote Ribrest."
Ribrest Framework is a simple Java Restful framework that truly improve your productivity when developing restful based webservices.
It works by automatically creating restful webservice endpoints based on model classes defined on your project. It frees you to focus on what really matters.
Any Java project has classes called model classes or domain classes. These classes are commonly real world representation and are used to store data.
For example, in a library application, the Book class is a nice example of a model class.
In most cases, model classes have at least four basic operations associated to it:
- Create new mode
- Read one or more models
- Update an existent model
- Delete an existent model
It's also known as CRUD.
In a restful webservice application, CRUD operations can be represented as the following http verbs:
POSTfor CGETfor RPUTfor UDELETEfor D
Of course model classes can have more than four operations, in fact it's rare when model classes have ONLY four basic operations. It can have as many operations it needs.
Ok, knowing that, what can Ribrest do for me?
Ribrest can scan your model classes and automatically generate valid and working endpoints by using a highly standardized structure.
You don't need to worry about webservices architectures, design patterns, data representation and other things anymore. Ribrest does all these things for you. Now you can focus on what is important to your project like domain classes, data validation and business logic.
For example, in a library application, just by to creating a Book class, Ribrest would generate the following working endpoints:
- http://localhost:2007/library/books/ - POST (To create a new Book)
- http://localhost:2007/library/books/ - GET (To read books)
- http://localhost:2007/library/books/{id} - PUT (To update an existent book)
- http://localhost:2007/library/books/{id} - DELETE (To delete an existent book)
These are valid endpoints! If you opened your browser and typed http://localhost:2007/library/books/, it would return a JSON representing all books stored in the database.
Yeah, you just create ONE CLASS called Book, and THAT'S IT! Everything is working. You will not hard code restful webservice resources anymore.
It's awesome! Don't you think?
Ribrest automatically scans your model class using Classgraph, creates programmatically endpoints using Jersey, setup and run a http server using Grizzly, manages dependency injection using HK2 and manages database ORM using JPA.
Thank you guys for all these fantastic projects.
Import the Ribrest as a maven dependency
<dependency>
<groupId>com.github.andtankian</groupId>
<artifactId>Ribrest</artifactId>
<version>1.26.2</version>
</dependency>
Create your new class following this basic setup:
@RibrestModel
@Entity
public class MyModelObject extends AbstractModel {
//some attributes and methods...
}
Create your main file to initialize the framework
public class MyMain {
public static void main (String args[]){
Ribrest.getInstance()
.appBaseUrl("http://localhost:2007")
.appName("myapp")
.persistenceUnitName("myunit")
.init();
}
}
That's it. The minimal requirement to run Ribrest based projects :)
DON'T FORGET THE FOLLOWING THINGS
- You should have a persistence unit like
resources/META-INF/persistence.xmlnamed by your choice. If you like, you can copy, paste and modify the Ribrest test persistence unit. You can find it here. - No programs or servers should be running in the port you defined in your code. It's very common, in development environment, to have others servers running in the port 3000, 5000, 8000, 8080. Make sure the port specified in your code be unique.
Ok, making sure of above tenets, you can now run your Ribrest project and watch the following output log.
Ribrest INFO: *** INITIALIZING RIBREST FRAMEWORK ***
Ribrest INFO: Application is up and running at: http://localhost:2007/myapp/
Ribrest INFO: Static file server has been created at: http://localhost:2007/static
Ribrest INFO: To shutdown Ribrest, type CTRL+C...
Try to request to MyClass endpoint that should be: http://localhost:2007/myapp/mymodelobjects
| Verb | Request Headers | Expected Results |
|---|---|---|
| GET | - | Status:200, Content-Type: application/json (when there are objects registered) Status:204 (when there aren't objects registered) |
| POST | Content-Type: application/x-www-form-urlencoded | Status:201, Content-Type: application/json |
| PUT | Content-Type: application/x-www-form-urlencoded | Status:200, Content-Type: application/json |
| DELETE | Content-Type: application/x-www-form-urlencoded | Status:200, Content-Type: application/json |