These are the parameters I set. You don’t need to add any dependency yet, as we will be adding them in the next few steps. Click on Generate and you’ll get a zip file with the empty project.
2. Open generated project with IntelliJ Idea
To make sure we are all on the same page, this is what my build.gradle.kts
file contains. Due to Gradle being familiar we can recognize things like the Kotlin plugin and dependencies as well as the Java 1.8 that enables Java 8 language features in our project.
3. Creating the first Controller
A controller is the one that handles a request from our clients and returns a response with the information requested. To keep the introduction brief, you can find more information about Spring controllers here.
Let’s start by adding the dependency for all the Spring Boot web resources (including controllers) in our build.gradle.kts
dependencies.
implementation("org.springframework.boot:spring-boot-starter-web" )
After syncing Gradle, let’s proceed with our HelloController
class. For showcasing purposes, I’ve added some examples about how to add parameters, send 200 responses with JSON objects and send errors with custom messages.
To test that our first web service is working, we first need to build it into our local machine, we can do that by running the gradle command bootRun
After building it, we can test that it is working as expected using this curl
command.
curl --location --request GET 'http://localhost:8080/mobileApi/helloWorld'
If you prefer to use a GUI and also to keep your APIs organized and synced, I recommend testing your API by using the free client Postman. In the next image you can see how to test the controller we just created.
That’s it for the introductory setting up post. Jump to the next one to secure our APIs with OAuth2 and test our public and private calls with access_token
and refresh_token
.
How to create a REST API for your App with Spring Boot, Kotlin & Gradle (Part 2: Security with… |
![]() |
This article is part of a series of tutorials:
- Part 1. First Controller
- Part 2. Securing with OAuth2
- Part 3. Adding a H2 database
- Part 4. Testing the API
- Part 5. Deploy on Heroku
The entire code for this part is available on Github:
cvillaseca/mobileAPI |
![]() |
Remember to follow, share & hit the 👏 button if you liked it! :)
Thanks to Mario Sanoguera de Lorenzo.