系统要求
● Java 8 & 兼容java14 .
● Maven 3.3+
修改maven配置
找到maven的配置文件(根据你自己本地下安载的maven路径)F:\maven\apache-maven-3.9.2-bin\apache-maven-3.9.2\conf\settings.xml
这里我们主要修改两段配置:
- mirrors 替换为阿里云镜像
- profiles 使用jdk1.8进行编译
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.8</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>
</profile>
</profiles>
HelloWorld
需求:浏览发送/hello请求,响应 Hello,Spring Boot
确认Idea的Maven配置
这里指定我们自己安装的maven,和maven配置文件settings.xml
创建Maven项目
Idea — new project — Maven
pom.xml配置
1.导入springboot父工程:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.13</version>
</parent>
2.添加web依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
添加了web依赖后,可以看到idea左侧面板的External Libraries中多了很多jar包,这些都是跟web开发相关的依赖。
同时,我们也可以通过idea的右侧面板的Maven选项卡中查看到下载的依赖列表:
完整的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.maxiaoke</groupId>
<artifactId>boot-01-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<!--导入SpringBoot父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.13</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
编写SpringBootApplication入口类
package:com.maxiaoke.boot
class:MainApplication
src/main/java/com/maxiaoke/boot/MainApplication.java
package com.maxiaoke.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
主程序类
*
@SpringBootApplication
这是一个SpringBoot应用
*/
@SpringBootApplication
public class MainApplication
{
//主方法
public static void main(String[] args)
{
SpringApplication.run(MainApplication.class,args);
}
}
创建控制器类
src/main/java/com/maxiaoke/boot/controller/HelloController.java
package com.maxiaoke.boot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//@Controller
//@ResponseBody
/**
*
@RestController是@Controller与ResponseBody的合体
*
直接将字符串返回给浏览器
*/
@RestController
public class HelloController
{
@RequestMapping("/hello")
public String handle01()
{
return
"Hello SpringBoot.";
}
}
运行应用
运行入口类的main方法:
查看控制台默认启动的8080端口:
通过浏览器访问 localhost:8080 查看
更多springboot学习内容,就在码小课: www.maxiaoke.com