In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to build a SpringBoot project. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Zero, environment introduction
Environment: jdk1.8 and above, maven,Win10,IDEA, network
First, hand-in-hand creation
Only when the request is created in the package where the startup class resides, can it be started smoothly.
1. Creation step
Look at the picture. Just have hands.
The result is a maven project with the following directory structure:
Then add dependencies, the core dependencies of springboot. SpringBoot provides a component called spring-boot-starter-parent, which has a variety of commonly used dependencies (not all) into the management of the version, our project needs to take this project as the parent project, so that we do not have to worry about the dependent version, what dependencies we need, just lead into the coordinates!
Com.example demo 0.0.1-SNAPSHOT 4.0.0 11 org.springframework.boot spring-boot-starter-parent 2.5.4 org.springframework.boot spring-boot-starter-web
It is important to note that we do not specify version information here. Because the parent project of SpringBoot already manages the version. At this point, we will find that there are a lot of dependencies in the project:
These are automatically introduced by SpringBoot based on the dependency of spring-boot-starter-web, and all versions have been managed so that there are no conflicts.
two。 Startup classes and test authoring 2.1 project structure
The startup class reads the configuration information that needs to be used later. For example, when one of our applications is launched and the subsequent access to the application, the data is connected and the path information is available, just wait for the request to come. Similarly, when the startup class starts, you need configuration information in its subdirectory, such as database connections, such as @ RequestMapping (value= "queryList"), which should be read at startup.
2.2 create the startup class DemoApplicationpackage com.pdh;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DemoApplication {public static void main (String [] args) {SpringApplication.run (DemoApplication.class,args);}}
Then you can create a controller for testing:
Package com.pdh.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class TestController {@ GetMapping ("/ hello") public String hello () {return "hello";}} 2.3 Test
After starting DemoApplication, the browser accesses localhost:8080/hello and returns the data successfully.
Second, rely on tools to create
Select Spring Initializr, which uses https://starts.spring.io by default to quickly build spring boot projects, but cannot rely on this build when there is no network. It is also not realistic to build springboot projects without a network, because you need to use projects such as maven to access their remote repositories. Here are the detailed steps of the construction
File-> New-> Project, which is consistent with the above build, and then you can see the figure:
Here we choose https://start.spring.io by default as the default build path, of course, you can also choose the site you want to style. Here are some details, configuration, etc.
After clicking next
Because I'm just demonstrating, I'll just check the spring web dependency in web.
Now that the project is built, let's talk about the springboot project structure:
After that, you get the runnable springboot project. And write TestController, be sure to write it under the package where the main method is located and its subpackages
Then click to run the main method, open the browser, access localhost:8080, and enter to access it successfully.
Here are some special points about the springboot project:
Springboot is fully annotated and elegant.
Pom.xml file: maven dependency file.
DemoAppication.java: this file contains the main function to start the application.
The main method: it is equivalent to an entry to the program. By calling the run method and delegating the business to springboot's SpringApplication class, SpringApplication will guide our application, start spring, and then start the tomcat web server that we have configured. DemoApplication.class is passed as a parameter to the run method to tell SpringApplication who is the main spring component.
Application.properties: an empty properties file that can be configured as needed.
Start spring, and then start the tomcat web server that we configured. DemoApplication.class is passed as a parameter to the run method to tell SpringApplication who is the main spring component.
Application.properties: an empty properties file that can be configured as needed.
Thank you for reading! This is the end of the article on "how to build a SpringBoot project". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.