如何使用不同的方式创建SpringBoot项目

#如何使用多种方式创建Spring Boot项目

PS: 最近在整理Spring Boot相关方面的知识结构,知识点,尊重作者的劳动成果,此文为转载整理,作者原文链接为☞原文

使用Spring Boot创建项目有三种方式:一是通过在官网在线创建,二是通过idea和STS(eclipse的一个插件,全名spring tools suit,这款集成了spring相关的组件),三是通过创建普通的maven工程,添加注解即可,下面来具体看看

  • 1、通过Spring Boot官网在线创建
  • 2、通过idea或者STS工具创建Spring Boot项目
  • 3、创建普通的maven项目,使用注解生成Spring Boot项目
  • 4、使用原生的Maven命令创建SpringBoot项目。

一、通过Spring Boot官网在线创建;

这里提供一个在线生成springboot项目的地址,Spring Boot官网

这里以java为例,生成一个springboot项目做简单的介绍

点开options,我们来看看

添加依赖,可以搜索,也可以挨个的查找.

将下载好的springboot项目解压后放到你的idea工作空间中,在idea中导入即可

来看看项目结构

至此就完后才能了在线创建springboot项目了


##2、通过idea或者STS工具创建Spring Boot项目

打开Idea,点击file——>new——>project

选择Spring Initializr,点击下一步

项目构建信息

项目目录结构

至此创建完毕,STS类似,这里不做过多介绍;


##3、创建普通的maven项目,使用注解生成Spring Boot项目

点击file下的new,然后选择project

选择maven,点击下一步

填写项目名称以及组织ID,点击下一步

项目结构

现在还是一个简单的maven项目,还不是springboot项目,我们需要在pom.xml文件中添加依赖

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

pom.xml

<?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.zhouym</groupId>
    <artifactId>Heber</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

添加成功后,再在 java 目录下创建包,包中创建一个名为 App 的启动类,如下:

package com.zhouym.heber;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈〉
 *
 * @author zhouym
 * @create 2019/8/5
 * @since 1.0.0
 */

@EnableAutoConfiguration
@RestController
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }

    @GetMapping(value = "/query",produces = "text/html;charset=UTF-8")
    public String query(){

        return "hello,中国";
    }
}

@EnableAutoConfiguration 注解表示开启自动化配置。

然后执行这里的 main 方法就可以启动一个 Spring Boot 工程了。

来看看结果


4、补充: 以Maven命令的形式创建Spring Boot项目;

1)、命令参数解析:

以下优先分析一下Maven的相关命令和参数:

mvn archetype:generate:

Maven插件原型是一个Maven项目模板工具包。 

####-DgroupId          
包名

####-DartifactId        
项目名

####-DarchetypeArtifactId  
类型maven-archetype-quickstart,创建一个Java Project,maven-archetype-webapp,创建一个Web Project

####-DinteractiveMode
是否使用交互模式,如果为false,非交互式的命令后直接创建,否则会有控制台提示输入操作

2)、使用命令参数创建项目

指定项目创建的目录;

本文指定项目创建的目录为: 
N:\技术学习整理\技术学习总结整理\Spring Boot学习总结整理——博客\项目目录

输入mvn命令

mvn archetype:generate -DinteractiveMode=false -DgroupId=com.xcy -DartifactId=springboot -Dversion=1.0.0-SNAPSHOT

执行命令日志为

N:\技术学习整理\技术学习总结整理\Spring Boot学习总结整理——博客\项目目录>mvn archetype:generate -DinteractiveMode=false -DgroupId=com.xcy -DartifactId=springboot -Dversion=1.0.0-SNAPSHOT
[INFO] Scanning for projects...
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.pom (15 kB at 9.6 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-plugins/16/maven-plugins-16.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-plugins/16/maven-plugins-16.pom (13 kB at 22 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.jar (209 kB at 217 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-dependency-plugin/2.8/maven-dependency-plugin-2.8.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-dependency-plugin/2.8/maven-dependency-plugin-2.8.pom (11 kB at 22 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-dependency-plugin/2.8/maven-dependency-plugin-2.8.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-dependency-plugin/2.8/maven-dependency-plugin-2.8.jar (153 kB at 192 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-release-plugin/2.5.3/maven-release-plugin-2.5.3.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-release-plugin/2.5.3/maven-release-plugin-2.5.3.pom (11 kB at 21 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/release/maven-release/2.5.3/maven-release-2.5.3.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/release/maven-release/2.5.3/maven-release-2.5.3.pom (5.0 kB at 9.7 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-release-plugin/2.5.3/maven-release-plugin-2.5.3.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-release-plugin/2.5.3/maven-release-plugin-2.5.3.jar (53 kB at 50 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-metadata.xml
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/mojo/maven-metadata.xml
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-metadata.xml (9.9 kB at 4.0 kB/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/mojo/maven-metadata.xml (21 kB at 8.3 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml (919 B at 415 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-archetype-plugin/3.1.2/maven-archetype-plugin-3.1.2.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-archetype-plugin/3.1.2/maven-archetype-plugin-3.1.2.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/maven-archetype/3.1.2/maven-archetype-3.1.2.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/maven-archetype/3.1.2/maven-archetype-3.1.2.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-archetype-plugin/3.1.2/maven-archetype-plugin-3.1.2.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-archetype-plugin/3.1.2/maven-archetype-plugin-3.1.2.jar (0 B at 0 B/s)
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-catalog/3.1.2/archetype-catalog-3.1.2.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-catalog/3.1.2/archetype-catalog-3.1.2.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-models/3.1.2/archetype-models-3.1.2.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-models/3.1.2/archetype-models-3.1.2.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-descriptor/3.1.2/archetype-descriptor-3.1.2.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-descriptor/3.1.2/archetype-descriptor-3.1.2.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-common/3.1.2/archetype-common-3.1.2.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-common/3.1.2/archetype-common-3.1.2.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/groovy/groovy/2.4.16/groovy-2.4.16.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/groovy/groovy/2.4.16/groovy-2.4.16.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/ivy/ivy/2.4.0/ivy-2.4.0.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/ivy/ivy/2.4.0/ivy-2.4.0.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/net/sourceforge/jchardet/jchardet/1.0/jchardet-1.0.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/net/sourceforge/jchardet/jchardet/1.0/jchardet-1.0.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-invoker/3.0.1/maven-invoker-3.0.1.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-invoker/3.0.1/maven-invoker-3.0.1.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-artifact-transfer/0.11.0/maven-artifact-transfer-0.11.0.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-artifact-transfer/0.11.0/maven-artifact-transfer-0.11.0.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon-provider-api/3.3.3/wagon-provider-api-3.3.3.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon-provider-api/3.3.3/wagon-provider-api-3.3.3.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon/3.3.3/wagon-3.3.3.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon/3.3.3/wagon-3.3.3.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-script-interpreter/1.0/maven-script-interpreter-1.0.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-script-interpreter/1.0/maven-script-interpreter-1.0.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/groovy/groovy/1.8.3/groovy-1.8.3.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/groovy/groovy/1.8.3/groovy-1.8.3.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/ant/ant/1.8.1/ant-1.8.1.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/ant/ant/1.8.1/ant-1.8.1.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/ant/ant-parent/1.8.1/ant-parent-1.8.1.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/ant/ant-parent/1.8.1/ant-parent-1.8.1.pom (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-catalog/3.1.2/archetype-catalog-3.1.2.jar
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-common/3.1.2/archetype-common-3.1.2.jar
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/groovy/groovy/2.4.16/groovy-2.4.16.jar
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-descriptor/3.1.2/archetype-descriptor-3.1.2.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-catalog/3.1.2/archetype-catalog-3.1.2.jar (0 B at 0 B/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/net/sourceforge/jchardet/jchardet/1.0/jchardet-1.0.jar
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon-provider-api/3.3.3/wagon-provider-api-3.3.3.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/net/sourceforge/jchardet/jchardet/1.0/jchardet-1.0.jar (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-invoker/3.0.1/maven-invoker-3.0.1.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon-provider-api/3.3.3/wagon-provider-api-3.3.3.jar (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-artifact-transfer/0.11.0/maven-artifact-transfer-0.11.0.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/groovy/groovy/2.4.16/groovy-2.4.16.jar (0 B at 0 B/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-descriptor/3.1.2/archetype-descriptor-3.1.2.jar (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-script-interpreter/1.0/maven-script-interpreter-1.0.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/archetype-common/3.1.2/archetype-common-3.1.2.jar (0 B at 0 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/ant/ant/1.8.1/ant-1.8.1.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-invoker/3.0.1/maven-invoker-3.0.1.jar (0 B at 0 B/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-artifact-transfer/0.11.0/maven-artifact-transfer-0.11.0.jar (0 B at 0 B/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-script-interpreter/1.0/maven-script-interpreter-1.0.jar (0 B at 0 B/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/ant/ant/1.8.1/ant-1.8.1.jar (0 B at 0 B/s)
[INFO] Generating project in Batch mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 1.4 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles-2.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles-2.pom (1.5 kB at 2.3 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1.pom (1.3 kB at 2.6 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-parent/4/maven-parent-4.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-parent/4/maven-parent-4.pom (10.0 kB at 12 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (4.3 kB at 8.4 kB/s)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: N:\技术学习整理\技术学习总结整理\Spring Boot学习总结整理——博客\项目目录
[INFO] Parameter: package, Value: com.xcy
[INFO] Parameter: groupId, Value: com.xcy
[INFO] Parameter: artifactId, Value: springboot
[INFO] Parameter: packageName, Value: com.xcy
[INFO] Parameter: version, Value: 1.0.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: N:\技术学习整理\技术学习总结整理\Spring Boot学习总结整理——博客\项目目录\springboot
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.110 s
[INFO] Finished at: 2020-06-20T14:33:04+08:00
[INFO] ------------------------------------------------------------------------
[0m[0m
N:\技术学习整理\技术学习总结整理\Spring Boot学习总结整理——博客\项目目录>

项目创建成功之后使用IDEA导入刚刚创建的项目

如果导入项目显示不出项目的目录 Ctrl+Shift+Alt+S 选择Modules→Sources→Apply→OK

在pom文件里添加Spring Boot依赖

对于SpingBoot测试Junit至少是4.1-4.2以上的版本,所以把版本号删除默认的就行

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>
<!--Spring Boot WEB依赖-->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

将App改变为启动类

spring boot提供了一个统一的注解**@SpringBootApplication。**
@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan

package com.xcy;

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

/**
 * Maven命令行创建Spring Boot项目启动类
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class,args);
    }
}

测试

必须要写在App启动类一个包下才能够扫描到
@RestController注解相当于@ResponseBody + @Controller合在一起的作用

package com.xcy.testdemo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @GetMapping("hello")
    public String index(){
        return "HelloWorld~";
    }
}

项目启动日志

使用postmain工具测试

在这里再次分享一篇写的不错的关于SpringBoot项目创建的博客☞ SpringBoot项目创建方式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章