本文主要介绍如何 手动创建第1个 SpringBoot2 简单应用——“HelloWorld” web 工程。具体可参考 springboot 官方文档中的 11. Developing Your First Spring Boot Application。目录结构如下:

PS: 2020-04-08更新,并附上helloworld工程代码地址

1、开发环境配置说明

  首先列一下自己的一些开发环境信息:
win10 + JDK 1.8.0_111 + Apache Maven 3.3.9 + idea2019.1/Eclipse Mars.2 Release (4.5.2) + 阿里云maven镜像(https://maven.aliyun.com/repository/public)
  需要注意的是,如果在命令行使用 maven 而且不指定配置文件的话,则用的是 maven 默认的镜像,地址:https://repo.maven.apache.org/maven2 ,建议通过参数【-s setting.xml的全路径】指定 maven 的配置文件,使用指定的镜像仓库和本地仓库。
  对于 JDK 和 maven ,先要确保没问题,命令分别为 java -versionmvn -v

2、创建 springboot2 简单工程

2.1、创建相关目录和 pom 文件

  首先就是先创建工程所需要的目录以及 pom.xml 文件,例如,我这里创建了 springboot2-example-helloworld 作为工程项目来使用,内部结构如下:

  当然还有个 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.wpbxin</groupId>
<artifactId>springboot2-example-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<!-- Additional lines to be added here... --> </project>

 然后使用命令 mvn package (注意:在 springboot2-example-helloworld 目录下)运行下确保打包正常(这里没有指定配置文件,使用的默认的 maven 镜像:https://repo.maven.apache.org/maven2 ,下载速度相对来说应该会慢点,稍微缓一缓休息下,正常就行,一次不行再来一遍。建议通过参数【-s setting.xml的全路径】指定 maven 的配置文件,使用指定的镜像仓库和本地仓库。例如:mvn package -s C:\your-maven-path\apache-maven-3.3.9\conf\settings-aliyun.xml):

  注意:如果打包时遇到了错误:“sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target” ,可以参考笔者的另一篇说明:Maven:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target,还有可能 Maven:java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty,可以解决相关问题。

2.2、添加依赖

  springboot 提供了很多的“Starters”来添加相关的 jar 包依赖,案例中的 spring-boot-starter-parent 是一个比较特殊的 parent pom ,它提供了很多有用的默认配置,但是没有任何的 jar 直接依赖。这里可以运行下 mvn dependency:tree 查看下当前工程的依赖,可以发现 spring-boot-starter-parent 并没有提供任何依赖。

  这里我们要创建的是 web 工程,需要有 web 相关的依赖,因此 pom.xml 中添加如下配置:

    <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

  再来一遍 mvn dependency:tree (截图的是已经下载完后的),这下可以看出 spring 全家桶差不多出来了,而且还有内嵌的 tomcat

2.3、添加“Hello World”代码

  HelloWorld Java类:

package com.wpbxin;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*; @RestController
@EnableAutoConfiguration
public class HelloWorldExample {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(HelloWorldExample.class, args);
}
}

2.4、运行“Hello World” web 工程

  通过命令 mvn spring-boot:run 来运行(期间笔者又遇到了 PKIX 的错误,重新来几遍就没问题了,很可能是网络问题):

  看到打出来了 Spring 的标识就正常了,然后访问 http://localhost:8080/ 正常:

  然后按 ctrl -c 结束:

2.5、创建可运行的jar

  pom.xml 中增加配置:

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

  再次执行 mvn package

  这时候 target 目录下会有个文件 springboot2-example-helloworld-0.0.1-SNAPSHOT.jar.original 和相关的jar 包(大概10MB左右):

  通过 jar -tvf target\springboot2-example-helloworld-0.0.1-SNAPSHOT.jar 查看内部所有文件和引用:

  这次通过 jar -jar 来运行 jar 包,也是正常:

  访问 http://localhost:8080/ ,OK

  同样是通过 CTRL-c 来结束运行:

  至此,整合了springboot2 的 “Hello World” web 工程便可正常使用了。

2.6、“HelloWorld” web 工程链接和 maven 的配置

  本文使用的工程参考github链接:springboot2-example-helloworld

  使用的 maven 的setting.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>C:\cs-softwares\maven-repo</localRepository>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<mirrorOf>central</mirrorOf>
<!-- 阿里云公共代理库使用指南:https://help.aliyun.com/document_detail/102512.html?spm=a2c40.aliyun_maven_repo.0.0.36183054oSYFKS -->
<!-- <url>https://maven.aliyun.com/nexus/content/groups/public</url> -->
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<!-- 默认的maven仓库-2019-10-26:https://repo.maven.apache.org/maven2 -->
</mirrors> <profiles>
<!-- 阿里云私服 -->
<profile>
<id>alimaven-profile</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>
<repositories>
<repository>
<id>alimaven</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</profile>
</profiles> <activeProfiles>
<activeProfile>alimaven-profile</activeProfile>
</activeProfiles>
</settings>

Spring Boot2(002):手动创建第1个SpringBoot2简单应用——“HelloWorld” web 工程的更多相关文章

  1. Spring Boot2(003):简要回顾“HelloWorld” web 工程

    1.注解: @RestController 和 @RequestMapping HelloWorldExample 中的第1个注解 @RestController 是一个被熟知的原型注解(stereo ...

  2. Spring Boot2(007):关于Spring beans、依赖注入 和 @SpringBootApplication 注解

    一.关于Spring beans 和 依赖注入(Dependency Injection) spring boot 和 Spring 全家桶无缝衔接,开发过程中可以很轻松地使用 Spring 全家桶的 ...

  3. Spring Boot2(006):关于配置类(Configuration Classes)和配置(Configuration)

    一.配置类(Configuration Classes) Spring Boot 支持基于 xml 的配置,但更偏向于使用基于 Java 的配置,通常建议使用定义有 main 方法的主 @Config ...

  4. Spring Boot2(005):关于代码结构

    spring boot 对于工程代码结构并没有特殊得要求,但以下几个有用的最佳实践建议参考参考: 1.不鼓励而且应该避免使用 default 包 没有 package 声明的类被认为是在 defaul ...

  5. Spring Boot2(004):关于 Build Systems (构建系统)

    Spring Boot Ref 建议使用 maven 或者 gradle 来进行依赖管理和应用构建. 一.Dependency Management(依赖管理) Spring Boot 的每个版本都会 ...

  6. 使用IDEA创建Spring boot项目,继承mybaits。并进行简单的数据库查询操作

    本文讲的是使用IEDA创建Spring boot项目,对于环境安装需要自行准备,如JDK1.8.Maven 3.3.IDEA编译器.Mysql5.7等需事前准备好. 1.创建Spring boot项目 ...

  7. 构建第一个Spring Boot2.0应用之项目创建(一)

     1.开发环境 IDE: JAVA环境: Tomcat: 2.使用Idea生成spring boot项目 以下是使用Idea生成基本的spring boot的步骤. (1)创建工程第一步 (2)创建工 ...

  8. Spring Boot2 系列教程(二)创建 Spring Boot 项目的三种方式

    我最早是 2016 年底开始写 Spring Boot 相关的博客,当时使用的版本还是 1.4.x ,文章发表在 CSDN 上,阅读量最大的一篇有 43W+,如下图: 2017 年由于种种原因,就没有 ...

  9. Spring Boot2.0 整合 Kafka

    Kafka 概述 Apache Kafka 是一个分布式流处理平台,用于构建实时的数据管道和流式的应用.它可以让你发布和订阅流式的记录,可以储存流式的记录,并且有较好的容错性,可以在流式记录产生时就进 ...

随机推荐

  1. Windows安装OpenSSH服务

    一.背景 在做国盛通项目的时候,有两套并行测试环境,因为基本架构采用的是供应商提供的程序,需要将两套banner图做同步,因为图片数量多,进GitLab版本控制进行分支策略管理,进而同步两套环境,意义 ...

  2. PAT (Advanced Level) 1132~1135:1132 模拟 1133模拟(易超时!) 1134图 1135红黑树

    1132 Cut Integer(20 分) 题意:将一个含K(K为偶数)个数字的整数Z割分为A和B两部分,若Z能被A*B整除,则输出Yes,否则输出No. 分析:当A*B为0的时候,不能被Z整除,输 ...

  3. express 配置 https 服务 ( 以阿里云服务器为例), 探索一周终于搞定

    首先最重要的是 你要明白 https接口的接收或者发送 的形式 是  https://域名:端口号   而不是 https://ip:端口号   一,首先,去阿里云注册免费ssl证书   1,在搜索框 ...

  4. Django null=True和blank=True的区别

    今天遇到一个问题: 在restframework框架中开发,数据库了创建了一个model的属性如下所示: remarks = models.CharField(verbose_name=u" ...

  5. 微信扫码登陆(JAVA)

    在web端用到weChat扫码登录,在手机扫码登陆成功后,跳转到相应的界面. 1.第一步请求code 调用接口:https://open.weixin.qq.com/connect/qrconnect ...

  6. leetcode303 Range Sum Query - Immutable

    """ Given an integer array nums, find the sum of the elements between indices i and j ...

  7. java注解——内置注解和四种元注解

    java内置注解: @Override(重写方法):被用于标注方法,用于说明所标注的方法是重写父类的方法 @Deprecated(过时方法):用于说明所标注元素,因存在安全问题或有更好选择而不鼓励使用 ...

  8. CF97B Superset超级集合

    CF97B Superset 这题主要是构造难想.看看数据范围发现连\(O(n^2)\)都被卡了,然后 考试的名称提醒我 想到了分治. 坐标按横坐标为关键字排序后找中间的点进行分治不是点分治qwq. ...

  9. mysql IF-IFNULL和IF-ISNULL同样逻辑的运行差别

    首先,目标记录是存在的 SELECT * FROM d_device_user_bind dub WHERE dub.`uid`='222222222221' 其次, SELECT dub.uid,d ...

  10. NRF52840与NRF52832的性能区别

    蓝牙版本的不断更新,大部分客户慢慢都向往着蓝牙5.0.当然对于前不久NORDIC刚出的蓝牙5.0 NRF52840,很多人都还不是很了解.NRF52840可以说是NRF52832的超强升级版,虽然同样 ...