【springboot】IDEA创建SpringBoot简单工程(有插件)

需求

使用SpringBoot开发一个web应用,浏览器发起请求/hello后,给浏览器返回字符串 hello world~

步骤

1.创建Maven工程

2.导入spring-boot-stater-web起步依赖

3.编写controller

4.提供启动类

pom.xml文件了解

启动类

新建包

创建类

java 复制代码
package com.zwh.springbootquickstart.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello World";
    }
}

运行

端口号

结果

遇到的问题

问题1

问题2

问题3

问题出在Maven尝试从中央仓库(https://repo.maven.apache.org/maven2)下载commons-io:commons-io:jar:2.11.0时失败,并且这个失败被缓存到了本地仓库,导致后续的构建过程中不再尝试重新下载该依赖

解决

添加镜像使其需要的包完整下载

复制代码
<!-- 配置阿里云仓库 -->
<repositories>
    <repository>
        <id>aliyun-repos</id>
        <url>https://maven.aliyun.com/repository/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>aliyun-repos</id>
        <url>https://maven.aliyun.com/repository/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

问题4

警告信息指出,在未命名模块中调用了受限方法java.lang.System::load,并且建议使用--enable-native-access=ALL-UNNAMED参数来避免警告。如果不启用原生访问,未来版本中这些受限方法将被阻止。

给当前工程添加

复制代码
--enable-native-access=ALL-UNNAMED

--add-opens java.base/java.lang=ALL-UNNAMED

给模版添加,为了方便之后使用