Introduction to Springboot-Create a running Springboot project in IDEA

For more information, click to understand:  https://how2j.cn/k/springboot/springboot-idea/1641.html

table of Contents

Step 1: Run first, see the effect, and then learn

Step 2: Imitation and troubleshooting

Step 3: About SpringBoot version

Step 4: About Tomcat

Step 5: About the plugin

Step 6: create the project

Step 7: project parameters

Step 8: select the web module

Step 9: Specify the path of the project

Step 10: SpringbootApplication.java

Step 11: HelloController.java

Step 12: run and test


Step 1: Run first, see the effect, and then learn

The old rule is to download the runnable project in the download area (click to enter) first , configure it and run it, and then learn what steps have been taken to achieve this effect.
This is a maven project. For IDEA to import maven projects, please refer to the following steps: idea import maven style project
and run the class:   the main method of com.how2java.springboot.SpringbootApplication
Then visit the address:

http://127.0.0.1:8080/hello


You can see the effect as shown in the figure.
Note:  This maven project uses a lot of jar packages. If you think the jar package downloads slowly, you can use the library I am using: the maven repository used by the webmaster is a bit big~

Run first, see the effect, and then learn

Step 2: Imitation and troubleshooting

After ensuring that the runnable project can run correctly, follow the steps of the tutorial and imitate the code again. 
The imitation process will inevitably have code discrepancies, resulting in the failure to obtain the expected running results. At this moment, compare the correct answer  (runnable project) with your own code to locate the problem. 
In this way, learning is effective and troubleshooting is efficient , which can significantly increase the speed of learning and cross all barriers on the learning path. 

It is recommended to use diffmerge software for folder comparison. Compare your own project folder with my runnable project folder. 
This software is very powerful, you can know which two files in the folder are wrong, and you can clearly mark them. 
Here is a green installation and usage tutorial: diffmerge download and usage tutorial

Step 3: About SpringBoot version

This series of tutorials uses 1.5.9.RELEASE, now there is an updated Springboot 2.x version.
Their usage is similar, but there are still some differences.

I hope that after learning, mastering, and proficient on the basis of 1.5.9.RELEASE, if you are still interested, you can switch to the 2.x version by yourself.
What I want to emphasize here is that if you switch to the 2.x version now and run the code in the current springboot series of textbooks for the webmaster, unpredictable errors will occur, and finally create obstacles for your own learning.
Therefore, the webmaster shouted 3 sentences in a row:  Don't modify the version number at will! Do not modify the version number at will! Do not modify the version number at will!

Step 4: About Tomcat

Students who  run first, see the effect, and then learn to run successfully, may find it strange. This is obviously a web program running, why not start tomcat? It is the main method of a Java class that is started? 
This is because  the main method of the  com.how2java.springboot.SpringbootApplication class embeds tomcat, and there is no need to manually start tomcat.

Step 5: About the plugin

First of all, the Springboot application development in IDEA is the same as in Eclipse, which is essentially a maven project. However, IDEA itself comes with plug-ins that support SpringBoot. Unlike Eclipse, plug-ins need to be installed from a third party, which is very slow (foreign plug-in sources). So this knowledge point is developed using the SpringBoot plugin that comes with IDEA.

Step 6: create the project

Menu -> New -> Project -> Spring Initializr and click Next

Create project

Step 7: project parameters

Enter the parameters in the two places as shown in the figure, other parameters do not need to be modified, and then Next

Project parameters

Step 8: select the web module

Then select Web on the left, and only check Web on the right, then click Next

Select Web module

Step 9: Specify the path of the project

Specify the project path as  e:\project\springboot  (other locations are also possible).
After doing this, the project is created successfully, and the project structure can be seen.

Note:  This maven project uses a lot of jar packages. If you think the jar package downloads slowly, you can use the library I am using: the maven repository used by the webmaster is a bit big~

Specify the path of the project

Step 10: SpringbootApplication.java

After the project is created, it comes with a Springboot Application, which is marked by @SpringBootApplication, indicating that this is a Springboot application

SpringbootApplication.java

Step 11: HelloController.java

Create a new package  com.how2java.springboot.web , and then create a new class HelloController under it.
This class is an ordinary controller in Spring MVC.
@RestController is a new annotation in spring4, short for @ResponseBody and @Controller.

package com.how2java.springboot.web;

 

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

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

 

@RestController

public class HelloController {

 

    @RequestMapping("/hello")

    public String hello() {

        return "Hello Spring Boot!";

    }

 

}

Step 12: run and test

Next, run  SpringbootApplication.java , and then visit the address

http://127.0.0.1:8080/hello


You can see the test results

Run and test


For more information, click to understand:  https://how2j.cn/k/springboot/springboot-idea/1641.html

Guess you like

Origin blog.csdn.net/java_zdc/article/details/105620403