一 使用idea创建第一个springboot项目
使用IDEA创建一个Spring Boot项目有很多方法,这里介绍使用Maven和Spring Initializr这两种方法来创建一个新Spring Boot项目
方式一:使用Maven新建Spring Boot项目
1. File --> New --> Project... --> Maven ,如下图所示
Project SDK下拉列表框中选择前面安装的 Java1.8,如果下拉列表框中不存在Java 1.8,可以单击New按钮,找到安装Java的位置,选择它。至于Maven中的archetype,不用勾选。然后单击“Next”
2. 给新工程起名spring-boot-hello,配置GAV
3.配置pom.xml
使用Maven,通过导入Spring Boot的 starter 模块,可以将许多程序依赖包自动导入工程中。使用Maven的parent POM,还可以更容易地管理依赖的版本和使用默认的配置,工程中的模块也可以很方便地继承它。使用如下代码清单所示的简单maven配置,基本上就能为一个使用Sping Boot开发框架的Web项目开发提供所需的相关依赖。
<?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>org.example</groupId>
<artifactId>spring-boot-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
这里只使用了一个依赖配置 spring-boot-starter-web 和一个 parent 配置 spring-boot-starter-parent。
4.创建Controller
如下是官方提供的一个最简单的Web示例程序,这个实例只使用了几行代码,虽然简单,但实际上这已经算是一个完整的Web项目了。
详情如下:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ApplicationDemo {
@RequestMapping("/")
String home() {
return "hello";
}
public static void main(String[] args) {
SpringApplication.run(ApplicationDemo.class,args);
}
}
这个简单实例,首先是一个Spring Boot应用的程序入口,或者叫作主程序,其中使用了一个注解@SpringBootApplication来标注它是一个Spring Boot应用,main方法使它成为一个主程序,将在应用启动时首先被执行。其次,注解 @RestController 同时标注这个程序还是一个控制器,如果在浏览器中访问应用的根目录,它将调用 home 方法,并输出字符串:hello。
5.在IDEA环境中运行,浏览器访问效果如下
注意:如上只能在IDEA环境中运行,那么如何打包发布到环境外运行?需要在项目maven配置增加一个发布插件来实现。增加一个打包插件:spring-boot-maven-plugin
如下所示:
<?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>org.example</groupId>
<artifactId>spring-boot-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.12</version>
<relativePath/> <!-- lookup parent from repository -->
</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>
打包成功后,在工程的target目录中将会生产jar文件spring-boot-hello-1.0-SNAPSHOT.jar。在命令行窗口中切换到target目录中,运行如下指令,就能启动应用。
java -jar spring-boot-hello-1.0-SNAPSHOT.jar
方式二: 使用Spring Initializr 新建Spring Boot项目
1. File --> New --> Project... --> Spring Initializr ,如下图所示
指定jdk,Choolse Initializr Service URL 保持默认即可。然后,next.
2. 在弹出的 Project Metadata 窗口中,Version: 默认是 0.0.1-SNAPSHOT ,我习惯指定为 1.0.0
3. Spring Boot版本选择2.1.13 ,依赖只选择 一个 Web --> Spring Web 即可。
4. 指定工程路径,Finish
5. 选择 New Window
6. 生成的项目结构如下
其中 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.hello</groupId>
<artifactId>hello-world</artifactId>
<version>1.0.0</version>
<name>hello-world</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
HelloWordApplication 这个类内容如下所示:
package com.example.hello.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
7. 在com.example.hello.helloworld 包下创建一个Controller
给HelloController 类添加 @RestController注解,并在类里面创建一个 hello()方法,给hello()方法添加 @GetMapping注解
package com.example.hello.helloworld;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello World! 这是我创建的第一个SpringBoot项目。";
}
}
至此,咱们的第一SprinBoot项目使用idea创建完成。
二 如何启动springboot项目
启动方式1:
启动服务,点击如图所示右上角绿色三角形启动服务。
启动说明:通过默认配置启动的是8080端口
浏览器访问网址 http://localhost:8080/hello , 效果如下
启动方式2:
找到 HelloWorldApplication 类
三 如何指点端口和上下文
若想指定端口和上下文(默认端口是8080),可在 resources 目录下找到一配置文件 application.properties (默认该文件为空文件,文件没有任何内容)
在 application.properties 文件中指定 端口 号 和上下文。
application.properties 文件内容 如下:
server.port=9090
server.servlet.context-path=/say-hello
eg:
也可以使用application.yaml 文件来代替 application.properties 文件。
application.yaml文件中指定端口和上下文写法如下:
server:
port: 9090
servlet:
context-path: /say-hello
eg:
启动服务:
访问url: http://localhost:9090/say-hello/hello 效果如下:
注意:
如果将默认生成的 application.properties文件删掉。服务仍可正常启动,不过端口和上下文变成了默认的(其实resources整个文件夹都可以删掉)。如下图: