Spring Boot 001: Set up and Ngrok

tahaburak.koc
4 min readNov 24, 2019

Hello. I’ve been planning to start an article series here on Medium for a while now and decided tonight is as good as any. Title says “001” but I can assure you I’m not planning to release hundreds of articles, it’s just a simple habit of mine.

I feel obligated to tell that I’m not a native speaker (and a bit rusty these days) therefore articles going to contain lots of typos and (worse) meaningless sentences. All I can hope is that project itself and its code makes it up.

At the moment all I know about this project that it’ll be a Spring Boot project that does CRUD operations with libraries and tools that I find useful.

After this long introduction let’s get started.

Spring Boot logo

0. Prerequisites and Notice

I’d like to assume it’s not your first project and you have GIT, maven, an IDE installed on your computer. I’ll be be using IntelliJ IDEA by Jetbrains(it has a community edition and I believe it’s the future so check it out if not already) and macOS as platform so I’ll be able cover tools on these.

1. Initialization

There are couple of ways to initialize a Spring Boot project. However most common ones are either to use IDE of your choice or https://start.spring.io/. I prefer the second option so I’ll continue from there.

https://start.spring.io/ with Halloween theme

Thanks to this tool we can create a Spring Boot in seconds with the dependencies that we want to include. In order to follow up just click this link, rename the project and download.

  • For this article only dependency that you need to include is Spring WEB. The dependencies in the pom.xml will look like this:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

When the download finishes just unarchive the file and you’ll have a folder as shown below:

Project’s folder

Since I’ll be using maven with Intellij IDEA its OK to delete .mvn folder, mvnw and mnvw.cmd files. You can keep HELP.md file in order to keep documentation up to date or delete it, too.

For any project I strongly suggest you to use GIT and if you’re, you’ll need a .gitignore file. Check https://www.gitignore.io/ to create one for this project or feel free to use this gist I use for Spring Boot projects. After updating .gitignore file run commands below on your terminal.

git init
git add .gitignore
git commit -m "gitignore created"
git add .
git commit -m "initial commit"

2. Playing Around

So we created the project with the dependencies and configured the GIT. From this point on we can work with the project on the IDE.

If the dependencies on the project not stored on your local machine IDE will start to download them online. Afterwards we can add the lines below to our application.properties file.

spring.application.name=atem
server.port=2836

Let’s create a simple GET controller as shown in order to test our application:

simple GET controller
package com.tahaburak.atem.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/** Created by burak on 4.11.2019 */
@RestController
@RequestMapping("")
public class BaseController {
@GetMapping(value = "ping")
@ResponseBody
public String ping() {
return "pong";
}
}

As you can see we simply create a GET controller that returns “pong” as response. Just run the application and visit http://localhost:2836/ping.

And don’t forget to commit the changes we’ve made.

git add .
git commit -m "created a simple get controller"

3. Ngrok

Basically ngrok is a tool that provides public URLs for exposing your local web server. Ngrok also provides a simple Web Interface to monitor requests that’s been made to your public URL.

Thanks to ngrok one can make the application accessible to anyone with internet connection without needing to have a server.

In order to use ngrok:

  • Visit its website at https://dashboard.ngrok.com/signup and signup.
  • Download the zip file for your platform and unzip it.
  • (Optinal) You can connect your account via running the command as given on dashboard
./ngrok authtoken {YOUR_TOKEN_FROM_DASHBOARD}
  • Simply run the command below:
./ngrok http 2836

As you can see ngrok provides a public URL to your localhost and a Web Interface at http://127.0.0.1:4040. You can simply copy the generated ngrok URL and test your project with other devices.

4. Conclusion

As you can see creating a new Spring Boot project and using ngrok with it takes around couple of minutes. So I don’t think reading this article was useful for one. However I’m planning to continue this project with other tools and libraries that’ll come in handy.

You can find the code at Github.

Sincerely,
Burak.

--

--