Spring Boot Tutorial - HelloWorld

Keywords: Java Spring Maven SpringBoot

1. Origin of Spring Boot

As we all know, the Spring Framework is a very important and lightweight framework in the Java ecology, helping our broad masses of big guys develop Java.The Spring Framework has evolved very rapidly, from the very beginning of the Spring core container that helped us manage Java objects to the various business issues that followed, the Spring Framework has almost a corresponding solution.However, as more and more businesses are integrated into the Spring framework, more and more configuration files for Spring and its components, and more and more complex configuration, developers can not focus on business logic very well, which is a problem for developers.So the Spring framework itself is lightweight, but Spring's configuration is heavy.Spring's developers have long noticed this problem, and Spring Boot was born to solve it.The core idea of Spring Boot is that conventions are better than configurations.That is, Java developers don't need to care about the dependencies between jar packages. Spring developers configure the dependencies for you in advance and make them into jar packages. Java developers just need to introduce jar to develop quickly, which greatly improves Java developers'efficiency, and there is only one configuration file left.Before Spring Boot came along, it might take about 30 minutes to build a project to develop a project, but after Spring Boot came along, no more than 5 minutes for developers to write code.Greatly improves developer productivity and injects new life into the Spring framework.

2. Spring Boot's hello world

How can we not learn such a good framework as Spring Boot?Next, I'll take you through Spring Boot, a framework that opens a new era and a new knowledge, which must start with hello world!

  • 2.1Create projects and introduce dependencies

    Create a project and trust that everyone will, omit this process and introduce maven dependencies directly.pom.xml:

    <!--springboot Parent Project-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <dependencies>
            <!--springboot frame web assembly-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.2.2.RELEASE</version>
            </dependency>
        </dependencies>
    
        <build>
            <!--springboot Of maven Plug-in unit-->
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <compilerArgs>
                            <arg>-parameters</arg>
                        </compilerArgs>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
  • 2.2Configuration of Profiles

    application.yml:

    server:
      port: 8080 # Applied Port
      servlet:
        context-path: /butterflytri # Mapping address for the entire application
    spring:
      application:
        name: helloworld # apply name
    
  • 2.3Coding

    • First create the package:

    • Establish HelloWorldApplication.java Class:

      package org.butterflytri;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      /**
       * @author: WJF
       * @date: 2020/5/15
       * @description: HelloWorldApplication
       */
      
      /**
       * {@link SpringBootApplication}: This comment helps you load the configuration file {@link'Application.ymlConfiguration of'},
       *                                The class identified by this annotation is the startup class, which is the entry to the entire project.
       */
      @SpringBootApplication
      public class HelloWorldApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(HelloWorldApplication.class,args);
          }
      
      }
      
    • Establish HelloWorldController.java Class:

      package org.butterflytri.controller;
      
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      /**
       * @author: WJF
       * @date: 2020/5/15
       * @description: Hello World!
       */
      @RestController
      @RequestMapping("/helloWorld")
      public class HelloWorldController {
      
          private static final String HELLO_WORLD = "Hello World!";
      
      
          /**
           * Run the project, access:http://localhost:8080/butterflytri/helloWorld/go to see hello world!
           * @return String
           */
          @RequestMapping("/go")
          public String go() {
              return HELLO_WORLD;
          }
      
      }
      
  • 2.4Start a project, access methods

    VisitHttp://localhost: 8080/butterflytri/helloWorld/go path, you can see the application response results on the page: Hello World!

3. Project Address

This project portal: spring-boot-helloworld

This tutorial will be updated all the time. If you think the blogger can write it, pay attention to it, or it will be easier to learn it next time.

Posted by frostyhorse on Tue, 19 May 2020 09:50:29 -0700