Spring Boot(一) Hello World

First, I see the Spring Boot

    Spring Boot is a new framework provided by Pivotal team, which is designed to simplify the new Spring initial set up and application development process. The framework uses a particular manner be configured so that the developer is no longer necessary to define the configuration of the template.

    Spring Boot is the starting point for all project-based development of Spring. Spring Boot is designed to get you up and running as quickly as possible and reduce your Spring application configuration file as much as possible

    This is a description of the Spring Boot on Wikipedia. In fact, in my opinion Spring Boot is a combination of Spring and a series of java technology stack frame plus the convention Greater Than Thought configuration. It is not a new technology is not a new framework, but a combination of various techniques, it is the default configuration of the many frameworks use. It appears, greatly improves the efficiency of the development java applications. Its features is a lightweight, pluggable, micro-services. Simultaneous idea and spring boot also subvert I have a .net programmer awareness of java (I was born .net programmer for java biggest impression is "Configuration", "Configuration", "Configuration" and difficult to use the sum of the IDE). The original Java development project can become an elegant up!

Second, the project to build

     Or build a Web project in SSM (SSH) era probably requires the following steps

  1. New Project

  2. Configuring Web.xml, loading Spring and Spring Mvc

  3. Configure a database connection Spring transaction configuration, the configuration mybatis

  4. Configuration read load profiles, open notes

  5. Configuring Log Files

  6. After configuration, deployment debugging Tomcat

    ...

    A bunch of configuration, see big head, but also to many beginners prohibitive.

    And with Spring Boot, everything will be simple and elegant.

    This paper will start with Hello World, to build a simple Spring Boot Web project.

  7. File——New——Project(Moudle)

注意: 图中的https://start.spring.io/,也可以直接打开,在网站上直接生成项目下载,用idea打开也是一样的。

  1. Next将看到以下界面

  1. 继续下一步,选择Web->Spring Boot,在这里我们可以选择Spring Boot版本

  1. 继续下一步

  1. 点击完成将看到如下目录结构

至此,一个Spring Boot项目就创建完成。

三、项目结构说明

  1. 项目结构中主要目录说明大致如下
->spring-boot-web                        (项目或模块)
---> src
----->main
------->java                             (Java代码目录)
--------->spring.boot.web                 (包spring.boot.web)
----------->SpringBootWebApplication       (Spring Boot启动类)
------->resources
--------->static                         (静态资源目录)
--------->templates                      (视图模板目录)
--------->application.properties          (项目配置文件)
------->test                            (测试)
--------->java                      
----------->spring.boot.web
----------->SpringBootWebApplicationTests  (Spring Boot测试启动类)
--->pom.xml                             (maven pom配置文件)
  1. pom文件说明

    pom.xml,它是maven来管理各种jar包依赖的一个配置文件,maven相当于.net中的nuget,是一个包管理工具。我们可以看到项目搭建好之后,就默认为我们加上了下面这些配置。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--父节点配置了Spring Boot的一些信息,父节点代表子节点可以继承父节点的一些配置,如版本号,在这里配置了就不用在子节点配置了-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <!--项目信息-->
    <groupId>spring.boot.examples</groupId>
    <artifactId>spring-boot-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-web</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--依赖的包配置在下面这个节点-->
    <dependencies>
        <!--引入Spring Boot Web模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入Spring Boot 测试模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--Spring Boot Maven 插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  1. Spring Boot启动类
package spring.boot.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}

启动类是Spring Boot程序的入口。

四、Hello World

  1. 新建控制器

    我们在spring.boot.web下建一个controller包,包内新建HelloWorld类

    package spring.boot.web.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloWorld {
    
        @RequestMapping("/helloWorld")
        public String Hello() {
            return "Hello World!";
        }
    }
    

    这里有两个注解:

    @RestController的意思就是controller里面的方法都以json格式输出,不用再写什么jackjson配置的了!

    @RequestMapping配置Url映射

  2. 启动Spring Boot项目

    由于Spring Boot项目内置Tomcat服务器,我们不需要在部署到Tomcat。只需要要在配置文件application.properties里配置一下端口号,然后idea->run->run。

  server.port=8888

如下图,代表启动成功!

打开浏览器输入:http://localhost:8888/helloWorld

至此,Hello World项目完成。于Java上,再次体验到了简洁优雅的开发!

示例代码

Guess you like

Origin www.cnblogs.com/hunternet/p/11570538.html