Dropwizard is a popular java framework for restful service. It provides out of the box support for configuration, application metrics, logging and operational tools so the developers focus more on writing business logic and not on set ups.
This tutorial should help you set up your dropwizard project and few APIs.
Create Maven Project
Use your favourite IDE (Intellj or eclipse) and create a maven project. I will paste the screenshot for Intellij.
Click on Create New Project
Select Maven in the left-hand side pan.
Enter artifactid and groupid.
Read here to know about group id and artifact id.
Enter the project name.
Click Finish and you should see the project in your IDE. Add a file called dev.yml in your project. Your project structure should look like this.
Add Dropwizard Dependency in your pom.xml
Add Configuration file
Under src/main/java, as shown in the figure; create a java file called myConfiguration.java which extends Configuration.java. This file is wired to your dev.yml. This helps in changing the configuration without changing the code.
Your dev.yml should look like this:
Essentially, this means MyConfiguration.getUrl() will give me the url defined in dev.yml.
Add Main Method called MyApplication
Your project structure should look like this now:
You can now run your project with program argument server dev.yml in your run configuration. Your run configuration should look like this:
If you run it, you should see the url printed in the logs.
At this point, our dropwizard project is running. Now we will add get and post apis.
Add Resource File
Modify MyApplication.java to register your Resource.java
Your MyApplication.java should look like this:
Again run it. You should see the above-mentioned apis registered and printed in logs.
Please notice /resource/getName and /resource/postName in the logs above.
Your apis are now ready to be used.
Testing the apis
Please download postman
You can now test the apis. Screenshot of the get api being tested is pasted below: