Spring — how to make a first step in 2018

Hi, after some projects in Spring I had learned a lot about it and I would like to share some of my knowledge in Spring basis. There are many tutorials how to start with this framework, in official docs too, but when I was trying to start, I came across some obstacles, so today I decided to write my own step by step guide how to start with this framework.

I assume that you have at least very little knowledge about what the spring is, know basis of java, and you had used Intellij IDEA before.


So, let`s begin. Firstly, go to start.spring.io and in Dependencies part add “Web” dependency. Everything should look like this:

Then generate project. Open demo.zip and move folder “demo” to any place easy to find. Then open Intellij on that screen:

And import project. Search for location of demo folder and inside it check pom.xml. Path should look like this: D:\Projects\demo\pom.xml. Click OK and next a few times and then wait to Intellij do everything what it has to do.


When everything opens (sometimes it is done in 1 second if you have good computer or some minutes if bad) open DemoApplication,java in src/main/java/com.example.demo.

There can be something a little different in Intellij Community, because I have Pro version of it(if you are student, you can get it for free on jetbrains.com/student/).

Then create new class in com.example.demo named HelloController. On top of class declaration put @RestController annotation. It will mark the class as web controller, capable of handling the requests and bind methods return values to body of responses. On next, write public method returning String “Hello World”, named “greet” and marked with annotation @RequestMapping("/"). This annotation will tell Spring if someone will go to your site to empty URI, than it will call that method and returned value will be displayed. Whole class should look like this:

package com.example.demo;

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

@RestController
public class HelloController {

@RequestMapping("/")
public String greet() {
return "Hello World";
}
}

After that, start your application by clicking green arrow in DemoApplication.class and after Spring had started (in Run wait for message like “Started DemoApplication in 3.275 seconds (JVM running for 6.935)”) go in your browser to localhost:8080/ and there will be Hello World message.

Bravo, you wrote your first Spring program.

For little homework try to make your own project with your name (or nickname) and own project name, like com.hiosdra.verycreativenameofprojest Make in this project a controller returning integer. In starting point it will be 0 and every time someone will refresh this site, that integer will be incremented.


If you think this tutorial was good, give a clap or two and see you later on the next post.

Like what you read? Give Oskar Drozda a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.