Practical SpringBoot quick start tutorial

This article takes you to quickly get started with SpringBoot with actual combat graphics.

SpringBoot is a new open source lightweight framework that was developed by the Pivotal team in 2013 and the first version was released in April 2014. It is designed based on Spring 4.0, which not only inherits the original excellent features of the Spring framework, but also further simplifies the entire construction and development process of Spring applications by simplifying the configuration. In addition, SpringBoot integrates a large number of frameworks so that the version conflicts of dependent packages and the instability of references have been well resolved.

Features of SpringBoot:

1. Can create an independent Spring application, and based on its Maven or Gradle plug-in, you can create executable JARs and WARs;

2. Embedded Servlet container such as Tomcat or Jetty;

3. Provide an automatically configured "starter" project object model (POMS) to simplify Maven configuration;

4. Automatically configure the Spring container as much as possible;

5. Provide prepared features, such as indicators, health checks and external configuration;

6. Absolutely no code generation, no need for XML configuration.

In addition, the official website also provides Reference Doc. (Reference Documentation), API Doc. (API documentation), Samples, etc., which are first-hand materials for learning SpringBoot.

Next, we directly create a HelloWorld project. There are many ways to create a SpringBoot project. You can create it with Eclipse, IDEA and other tools, or you can create it directly through the online creation provided by the official website. Here we will explain it in the way of online creation.

First of all, you need to have JDK and IDEA locally (Maven can be downloaded separately or integrated with development tools). If you don’t know how to build a JDK environment, you can read my previous article " Introduction to Java (Windows, Mac, Linux, Docker) ", In addition, it should be noted that different versions of springboot have different requirements for JDK, Maven, Tomcat, etc., see the relevant reference documents https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started. html#getting-started.

There is a "Quickstart Your Project" under the overview page of the SpringBoot official website. Click " Spring Initializr " to enter the initial project interface.

Before many articles telling the Gradle project, has been created as a Maven project, which Dependencies on the right there is " the ADD DEPENDENCIES ..." you can add dependencies, fill out the other and finally click the bottom "in accordance with the actual needs GENERATE" to Generate and download the project.

After decompression, use Eclipse or IDEA to open the project. Here, use IDEA to open it. The first time you open it, you will be prompted to maven guide package, you can set automatic import, you can modify the maven version and local repository location.

If the dependency package cannot be downloaded and it reports "'parent.relativePath' points at no local POM @ line 5, column 10: Unknown host repo.maven.apache.org: nodename nor servname provided, or not known", directly put pom.xml " <relativePath/> " can be deleted or commented out:

The build could not read 1 project -> [Help 1]
  
  The project cn.xubingtao.springboot:helloworld:0.0.1-SNAPSHOT (/xxx/helloworld/pom.xml) has 1 error
    Non-resolvable parent POM for cn.xubingtao.springboot:helloworld:0.0.1-SNAPSHOT: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.4.1 from/to central (https://repo.maven.apache.org/maven2): Transfer failed for https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.4.1/spring-boot-starter-parent-2.4.1.pom and 'parent.relativePath' points at no local POM @ line 5, column 10: Unknown host repo.maven.apache.org: nodename nor servname provided, or not known -> [Help 2]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

If you use the previous maven and repository directories, it may be faster if some dependency packages are already downloaded. If you configure a new repository and follow the default configuration, it will be slower. You can use maven's settings.xml or the project's pom. Configure the remote warehouse in xml, and choose which remote warehouse to use according to actual needs. Many companies have their own warehouses.

Configure the remote warehouse in maven's settings.xml:

<!--配置远程仓库-->
    <mirror>
     <id>aliyun</id>
     <name>aliyun Maven</name>
      <mirrorOf>*</mirrorOf>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>

Configure the remote warehouse in the pom.xml of the project:

<repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

After downloading the dependent package, you can perform clean, test, install and other operations through the Maven menu on the right. This first creates a HelloWorldController controller so that you can access related content after running the project, and then run the startup class directly to access the related content in the browser , By the way, the install package project is packaged into a runnable jar package.

Right-click the target directory under the project to open the console and enter "java -jar helloworld-0.0.1-SNAPSHOT.jar" to run the packaged and runnable jar package of this project, and then enter the local IP plus the default 8080 in the browser Port and "/ hellWorld " can see the browser print out the relevant content.

So far you have started springboot.

Original link: https://www.xubingtao.cn/?p=2535

Follow my public account to release all kinds of useful information for you every day.

Guess you like

Origin blog.csdn.net/xubingtao/article/details/111151838