Spring Boot2(002):手动创建第1个SpringBoot2简单应用——“HelloWorld” web 工程
本文主要介绍如何 手动创建第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 -version 和 mvn -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 工程的更多相关文章
- Spring Boot2(003):简要回顾“HelloWorld” web 工程
1.注解: @RestController 和 @RequestMapping HelloWorldExample 中的第1个注解 @RestController 是一个被熟知的原型注解(stereo ...
- Spring Boot2(007):关于Spring beans、依赖注入 和 @SpringBootApplication 注解
一.关于Spring beans 和 依赖注入(Dependency Injection) spring boot 和 Spring 全家桶无缝衔接,开发过程中可以很轻松地使用 Spring 全家桶的 ...
- Spring Boot2(006):关于配置类(Configuration Classes)和配置(Configuration)
一.配置类(Configuration Classes) Spring Boot 支持基于 xml 的配置,但更偏向于使用基于 Java 的配置,通常建议使用定义有 main 方法的主 @Config ...
- Spring Boot2(005):关于代码结构
spring boot 对于工程代码结构并没有特殊得要求,但以下几个有用的最佳实践建议参考参考: 1.不鼓励而且应该避免使用 default 包 没有 package 声明的类被认为是在 defaul ...
- Spring Boot2(004):关于 Build Systems (构建系统)
Spring Boot Ref 建议使用 maven 或者 gradle 来进行依赖管理和应用构建. 一.Dependency Management(依赖管理) Spring Boot 的每个版本都会 ...
- 使用IDEA创建Spring boot项目,继承mybaits。并进行简单的数据库查询操作
本文讲的是使用IEDA创建Spring boot项目,对于环境安装需要自行准备,如JDK1.8.Maven 3.3.IDEA编译器.Mysql5.7等需事前准备好. 1.创建Spring boot项目 ...
- 构建第一个Spring Boot2.0应用之项目创建(一)
1.开发环境 IDE: JAVA环境: Tomcat: 2.使用Idea生成spring boot项目 以下是使用Idea生成基本的spring boot的步骤. (1)创建工程第一步 (2)创建工 ...
- Spring Boot2 系列教程(二)创建 Spring Boot 项目的三种方式
我最早是 2016 年底开始写 Spring Boot 相关的博客,当时使用的版本还是 1.4.x ,文章发表在 CSDN 上,阅读量最大的一篇有 43W+,如下图: 2017 年由于种种原因,就没有 ...
- Spring Boot2.0 整合 Kafka
Kafka 概述 Apache Kafka 是一个分布式流处理平台,用于构建实时的数据管道和流式的应用.它可以让你发布和订阅流式的记录,可以储存流式的记录,并且有较好的容错性,可以在流式记录产生时就进 ...
随机推荐
- jquery源码部分分析
1.整体架构和如何辨别浏览器端和node端 自执行函数,判断在什么端,如果在浏览器端就执行factory函数 //(function(){a,b})(a,b) //jq大架构,闭包,自执行函数,传入函 ...
- 时间转换(scanf的指定格式读入)
给定一个12小时制的时间,请将其转换成24小时制的时间.说明:12小时制的午夜12:00:00AM,对应的24小时制时间为00:00:00.12小时制的中午12:00:00PM,对应的24小时制时间为 ...
- 嵊州普及Day5T2
题意:将(w,h)的纸条折成(W,H),最少需几步. 思路:横竖互不干扰,然后最多可折int型一半,拿个函数判断两次比较即可,然后折不了的条件是需要的矩形大于给的矩形. 见代码: #include&l ...
- JMeter-响应断言设置
针对如上请求,可以设置3种相应断言: 1. 2. 3.
- spring事物(一),@EnableTransactionManagement @Transactional 启动解析
1.事物的声明阶段 @EnableTransactionManagement,是我们开启注解事物的第一步,我们来看下这个类为我们干了什么 @Target(ElementType.TYPE) @Rete ...
- s2010编译C++ 链栈的使用
// CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...
- Linux-hosts
Linux-hosts hosts文件 /etc/hosts OS hosts (path) 使其生效,命令行执行 Windows (C:\Windows\System32\drivers\etc\h ...
- cenos7配置confluence+mysql5.6
一.准备阶段 我的环境为 腾讯云镜像centos7.4 ,centos 内置 mariadb 需要先删除 #检查是否安装了 mariadb rpm -qa |grep mariadb #删除mari ...
- PowerShell中执行.net类库
Powershell脚本一个比较强大的功能是可以直接调用.net类库(ps core能调用.net core类库),除了调用系统类库外,也可以调用自己编写的类库,从而扩充我们脚本的功能.本文这里简单的 ...
- Ubuntu 终端命令速查表
1.man: shell命令的说明指南 该命令代表manual,提供一个给定命令的说明指南. 用法:man <shell command> 用例:man ls 上述命令请求命令‘ls’的说 ...