Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

How to create a REST API for your App with Spring Boot, Kotlin & Gradle (Part 1: Initial setup & Controller)

 

 
Cristian Villaseca
Software Engineer at Turo
Published: July 13, 2020
Tweet
Share
 

 

As an Mobile Engineer, I’ve tried numerous times to build an API for my App projects and I’ve always ended up frustrated for reasons like:

  • Not understanding the language or the framework I’m using.
  • Not willing to pay for hosting before the project goes live.
  • Using a platform that won’t scale up.

So after all these problems, I decided to write the Rest API using the Spring Boot framework, in Kotlin, and with Gradle as a dependency provider (instead of Maven). As an Android engineer I’m already familiar with Kotlin and Gradle. On the other hand, Spring Boot provides a fast way to set up an API & it also includes an embedded Tomcat. Another strong point towards Spring Boot is the community behind it which is highly active and has already solved scaling problems in the past.

 

1. Create Spring Boot project with Spring Boot project generator & the following parameters

 

Image for post

 

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.ktsfile 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 curlcommand.

 

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.

 

Image for post

 


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…

OAuth 2.0 is a protocol that allows a user to grant limited access to their resources on one site, to another site…

proandroiddev.com

 

 

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

REST API using Springboot 2.2.X written in Kotlin. Contribute to cvillaseca/mobileAPI development by creating an…

github.com

 

Remember to follow, share & hit the 👏 button if you liked it! :)

GitHub | LinkedIn | Twitter

 

Thanks to Mario Sanoguera de Lorenzo. 

 

 

 

Tags: Rest Api, Spring Boot, Kotlin, Gradle, Backend

 

View original article at: 


 

Originally published: June 18, 2020

Android News
Evolution of Android Update SystemEvolution of Android Update System
Evolution of Android Update SystemEvolution of Android Update System

By Ivan Kuten

So, how can you update Android on mobile devices? While developing software for Smart TVs and Android-based set-top boxes, we’ve narrowed it down to four ways, discarding some very exotic options:

By ProAndroidDev -
Android News
Happy Railway
Happy Railway

By Hadi Lashkari Ghouchani

This post is on the tail of Railway Oriented Programming in Kotlin by Antony Harfield. So you need to read it first and continue here. As it’s obvious I really liked it and tried it out. It needs every process have a result like

By ProAndroidDev -
Android News
Unit Tests and Concurrency
Unit Tests and Concurrency

By Stojan Anastasov

Once Retrofit added RxJava support, RxJava became my go-to concurrency framework for writing Android apps. One of the great things about RxJava is the excellent testing support. It includes TestObserver, TestScheduler, RxJavaPlugins so you can switch your schedulers in tests.

By ProAndroidDev -
Android News
When Compat libraries will not save you
When Compat libraries will not save you

By Danny Preussler

And why you should avoid using the “NewApi” suppression! The idea of “Compat” libraries was probably one of the key aspects of Android dominating the mobile space. Other than with iOS, Android users often could not update their operating system after a new version launch, simply as their phones won’t allow them to, the Android problem of fragmentation.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One