[Java] Display hello world with spring + gradle

3 minute read

In Construct Java environment and output helloworld, I wrote helloworld to the console. This time, let’s go a little further and display HTML in the spring project.

If you haven’t added spring yet, please refer to the previous articles. Add spring boot and gradle to Eclipse Install the plug-in on Eclipse

Create a #spring project First, let’s create a spring project.

  1. Select [file] => [new] => [Other] => [Spring Starter Project]
  2. Decide on a name for your project or package. This time I chose helloSpring. image.png

  3. Select project dependencies. I chose four this time. image.png

  4. Press finish to complete. I made it like this. image.png

What are dependencies?

Regarding the dependency described in 4. To be honest, I’m not sure, but it’s like choosing the Spring framework you want to use here. Is it an image of choosing what you ask Gradle-san? You can add it later by playing with the Gradle configuration file. (I would appreciate if you could let me know if you have a different perception)

Let’s take a look inside the created project. The configuration file of Gradle is build.gradle, so open it.

buid.gradle


plugins {
id'org.springframework.boot' version '2.3.2.RELEASE'
id'io.spring.dependency-management' version '1.0.9.RELEASE'
id'java'
}

group ='com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
mavenCentral()
}

dependencies {
       /* Added ↓ */
implementation'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation'org.springframework.boot:spring-boot-starter-web'
implementation'org.springframework.boot:spring-boot-starter-web-services'
developmentOnly'org.springframework.boot:spring-boot-devtools'
      /* Added ↑ */

testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group:'org.junit.vintage', module:'junit-vintage-engine'
}
}

test {
useJUnitPlatform()
}

The four selected when selecting the dependency are added. I don’t have enough knowledge about this area, so I would like to summarize it someday.

Output #helloworld Now that we have a project, let’s output it. Add two files. After adding it looks like this. image.png

1. HTML addition

Basically put the HTML file in templates in src/main/resources. Right-click templates => [New] => [Other] to create an HTML file, hello.

hello.html


<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
<h1>Hello World!!</h1>
</body>
</html>

2. Controller addition

Add a package called helloSpring.Controller and create HelloController.java in it.

HelloController.java


package helloSpring.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }
}

The point to note here is where to create the Controller. If you do not place the HelloSpringApplication.java of the main method of the project in the same package as the startup class or under it, it will not be loaded properly and a 404 error will occur.

You can create it in the helloSpring package, or be careful of the package name when adding a package for Controller as introduced this time.

What is ####Controller? MVC is one of the concepts for organizing application settings that is generally adopted in web frameworks, and it is the C part. I wonder what it is, but I will summarize the details later, so I will briefly explain it.

You can’t display HTML on the screen just by creating it, so you need a place to display it. Controller is the place to write the content that you want this processing when this URL comes.

Look at the code content

The ones with @ are called annotations. Annotation means an annotation.

By writing @Controller, this class is declared as Controller. In @RequestMapping, write the process of what kind of request comes in.

import is written to use these.

To explain this code in words from above (from @Controller), This is the Controller’s HelloController class. When there is a GET request for the URL “/”, the function hello is executed. is what it means. The hello function describes the process of returning a file called hello.

Execute

When you execute it, something like this will appear on the console. image.png

Since the port number is 8080, try accessing http://localhost:8080. image.png

I was able to output safely.

Next, I would like to change the description of MVC and output characters dynamically. Next => Under construction