安装maven maven官网 :
http://maven.apache.org/download.cgi
MacOS :
brew install maven
Linnux :
yum install maven
切换maven源 1 2 $> cd ~/.m2 $> vim settings.xml
将以下配置写入文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 <?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" > <mirrors > <mirror > <id > planetmirror.com</id > <name > aliyun</name > <url > http://maven.aliyun.com/nexus/content/groups/public/</url > <mirrorOf > central</mirrorOf > </mirror > </mirrors > </settings >
命令行创建项目 1 mvn archetype:generate -DgroupId=com.example -DartifactId=maven-demo04 -Dversion=1.0.0-SNAPSHOT -Dpackage=com.example
groupId :
一般命名规则 -> 公司名称反写+项目名称, 事实上,写啥都没毛病
artifactId :
一般命名规则项 -> 目名称-模块名称, 事实上,依旧可以随便写
version :
项目版本号, 按需自定义即可
package :
源码包目录
官方推荐groupId和package使用相同的路径
项目目录结构 |-- src
|-- main
|-- java
|-- com
|-- example
|-- App.java
|-- test(单元测试目录)
|-- pom.xml
引入spring-boot 打开 pom.xml
, 在 4.0.0 这一行下面添加如下内容
1 2 3 4 5 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.2</version> </parent>
然后执行 mvn install
安装相关依赖
安装完成后, 执行 mvn spring-boot:run
会看到控制台输出 Hello World!, 代表项目已经搭建成功
启动HTTP服务器
在 dependencies
中添加依赖
1 2 3 4 5 6 7 8 9 10 11 <!--web依赖包,web应用必备--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--@SpringBootApplication 注解需要--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.0.5.RELEASE</version> </dependency>
然后执行 mvn install
, 安装新引入的包
打开 App.java
, 在来上添加注解 @SpringbootApplication
, 同事, 主函数内容修改为 SpringApplication.run(App.class, args);
创建配置
配置读取的优先级 : program arguments > VM options > Enviroment variables > proties 文件 > yml文件
在main目录下, 新建 resources
目录, 同时创建如下两个文件
1 2 3 spring: profiles: active: dev
application-dev.yml : 内容为
此时在执行 mvn spring-boot:run
, 可以看到服务已经成功监听了 8081 端口
添加接口
在 App.java 同级目录添加 HelloWorld.java , 其内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.example;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map; @RestController @RequestMapping("/api" ) public class HelloWorld { @RequestMapping("/hello/world" ) public Map<String, Object> Hello() { Map<String, Object> data = new HashMap<>(); data.put("code" , 0 ); data.put("message" , "success" ); Map<String, Object> result = new HashMap<>(); result.put("say" , "hello world" ); data.put("data" , result); return data; } }
重新启动服务 : mvn spring-boot:run
访问接口 : http://localhost:8081/api/hello/world , 此时会看到如下返回值
1 2 3 4 5 6 7 { "code" : 0 , "data" : { "say" : "hello world" } , "message" : "success" }
至此, spring-boot web服务器已成功搭建完成!
常见问题 应用启动慢 1 InetAddress.getLocalHost().getHostName() took 5002 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
解决方法
hostname
查看主机名, 此处假定主机名 : temp.local
编辑 /etc/hosts
文件, 增加如下内容
1 2 3 127.0.0.1 temp.local 255.255.255.255 broadcasthost ::1 temp.local
启动报错 1 java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.httpPutFormContentFilter
解决方法
此处是因为 application 配置文件 与对应环境配置文件格式不一致, 如果使用 propertites
, 所有配置都需要是此格式, 如果使用 yml
, 则所有配置均需要是此格式