zoukankan      html  css  js  c++  java
  • springboot2专题之第二篇:Spring Boot 入门

    1、系统要求

    • Java 8 & 兼容java14 .
    • Maven 3.3+
    • idea 2019.1.2

    1.1、maven设置

    <mirrors>
          <mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>central</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
          </mirror>
      </mirrors>
     
      <profiles>
             <profile>
                  <id>jdk-1.8</id>
                  <activation>
                    <activeByDefault>true</activeByDefault>
                    <jdk>1.8</jdk>
                  </activation>
                  <properties>
                    <maven.compiler.source>1.8</maven.compiler.source>
                    <maven.compiler.target>1.8</maven.compiler.target>
                    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
                  </properties>
             </profile>
      </profiles>

    2、HelloWorld

    需求:浏览发送/hello请求,响应 Hello,Spring Boot 2

    参照官网:https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.first-application

    2.1、创建maven工程

    注意maven设置

     

     新建maven项目

     

     

    项目结构和pom文件

    2.2、引入依赖

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.4.RELEASE</version>
        </parent>
    
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
        </dependencies>

    完整pom

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.stu</groupId>
        <artifactId>springboot-helloword</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.4.RELEASE</version>
        </parent>
    
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
        </dependencies>
    </project>

    2.3、创建主程序

    如果是idea可以使用点的方式给类加包路径。

    启动类代码

    /**
     * 主程序类
     * @SpringBootApplication:这是一个SpringBoot应用
     */
    @SpringBootApplication
    public class MainApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MainApplication.class,args);
        }
    }

    2.4、编写业务

    @RestController
    public class HelloController {
    
    
        @RequestMapping("/hello")
        public String handle01(){
            return "Hello, Spring Boot 2!";
        }
    
    
    }

    2.5、测试

    直接运行main方法

    游览器访问http://localhost:8080/hello

    2.6、简化配置

    官网地址:https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties

    application.properties

    server.port=8888

    2.7、简化部署

    官网地址:https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.first-application.executable-jar

     <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

    添加后的完整pom文件

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.stu</groupId>
        <artifactId>springboot-helloword</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.4.RELEASE</version>
        </parent>
    
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>

    执行clean和package命令打包

    打包成功后生成一个jar包,如下图

    在生成的jar包地址里,cmd命令进入命令窗口

    执行java -jar springboot-helloword-1.0-SNAPSHOT.jar,启动后直接在游览器访问就可以了。

    把项目打成jar包,直接在目标服务器执行即可。

    注意点:

    • 取消掉cmd的快速编辑模式(防止鼠标点击的时候,启动服务卡住)

  • 相关阅读:
    web前端的发展态势
    AngularJs 简单入门
    css代码优化篇
    git提交报错:Please make sure you have the correct access rights and the repository exists.
    Activiti工作流框架学习
    遍历map集合的4种方法
    js设置日期、月份增加减少
    Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    webservice_rest接口_学习笔记
    相互匹配两个list集合+动态匹配${}参数
  • 原文地址:https://www.cnblogs.com/konglxblog/p/15547014.html
Copyright © 2011-2022 走看看