IntelliJ 创建Spring Boot项目

简介: 1). 新建工程 -> Creat New Project图1.png2). 选择模板Project SDK:点击New...选择jdkChoose Initializr Service URL 选择Custom, 链接选用http://start.
1). 新建工程 -> Creat New Project
img_4338ae6bd8a49f34d2d82e6ee2ac5953.png
图1.png
2). 选择模板
  • Project SDK:点击New...选择jdk
  • Choose Initializr Service URL 选择Custom, 链接选用http://start.spring.io/,据说不带s的快
    img_e2722acd496318d784b9f7b6105cb5aa.png
    图2.png
3). 配置
img_c7443accb0a291784b3977601ca3a761.png
图3.png
4). 选择Web -> web, (非必须选择)Template Engines -> Thymeleaf(用来替换jsp模板引擎)
img_8e17cb11ee8bfdb2241ca5844011b284.png
图4.png

img_1d197b2b462b94780a7fac3881e45002.png
图5.png
5). 选择工程名和路径
img_f8157574f649073442679c74afa42b3f.png
图6.png
6). 运行(点击绿色的三角按钮)
img_323d02f103bdd03891ee36b6e4c154a8.png
图7.png
img_ed4994373c46baee00538f53c0bdb5d2.png
图8.png
7). 浏览器打开http://localhost:8080

img_9766abe254e5fd42593edc51a84741d2.png
图9.png

原因
项目中没有静态页面及控制器.

8). 创建控制器
  • HelloController.kt
@Controller
@EnableAutoConfiguration
class HelloController {

    @RequestMapping("/")
    @ResponseBody
    fun index(): String {
        return "Hello World!"
    }
}
AI 代码解读

访问http://localhost:8080/

img_9607f0f2a4e1465a3a0411bc05e943fe.png
图10.png

9). 返回页面
  • index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    首页内容
</body>
</html>
AI 代码解读
  • HelloController.kt
@Controller
@EnableAutoConfiguration
class HelloController {

    @RequestMapping("/index.html")
    fun index() : String {
        return "index"
    }
}
AI 代码解读

访问http://localhost:8080/index.html

img_fd2b21ecad3429576d57800963ef7024.png
图10.png

10). 刷新配置
  • 修改pom.xml文件
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
   <scope>true</scope>
</dependency>
<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <configuration>
            <fork>true</fork>
         </configuration>
      </plugin>
   </plugins>
</build>
AI 代码解读
  • 修改idea
    I. Ctrl+Alt+S. Build,Execution,Deployment -> Compiler, 勾选Build project automatically.


    img_de53e3fd33147364a196bc605d5b3ed2.png
    图11.png

    II. Ctrl+Shift+Alt+ /


    img_6fec5fd132de606ae6b99424c82363bd.png
    图12.png

    img_50a32a964e1a2d66cd1aab14722f8e1b.png
    图13.png
  • 重新部署项目即可实现修改html刷新重载,修改kotlin代码重新部署
11). 使用模板引擎
  • 数据类Student
/**
 * 数据类
 */
data class Student (
        val name: String,
        val age: Int
)
AI 代码解读
  • 控制器Controller
@Controller
class HelloController {
    @RequestMapping("/students.html")
    fun students(map: MutableMap<String, Any>): String {
        val list = ArrayList<Student>()
        for (i in 0..9) {
            list.add(Student("张三$i", 23+i))
        }
        // 返回给页面的数据
        map["sList"] = list
        return "students"
    }
}
AI 代码解读
  • students.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>学生</title>
</head>
<body>
    所有学生
    <ul th:each="stu,stuSta:${sList}">
        <li>
            序号:<span th:text="${stuSta.index}"></span><br>
            姓名:<th:block th:text="${stu.name}"/><br>
            年龄:<div th:text="${stu.age}"></div><br>
        </li>
    </ul>
</body>
</html>
AI 代码解读

写完之后html代码报红线,使用Alt+Enter修复即可,也可不修复。(此为编辑器的问题)


img_ca42de02fd53266a9a19001ff41e6224.png
图14.png
  • 效果


    img_ba93857248e78348234c63c0caf694a7.png
    图15.png
目录
打赏
0
0
0
0
69
分享
相关文章
微服务——SpringBoot使用归纳——Spring Boot开发环境搭建和项目启动
本文介绍了Spring Boot开发环境的搭建和项目启动流程。主要内容包括:jdk的配置(IDEA、STS/eclipse设置方法)、Spring Boot工程的构建方式(IDEA快速构建、官方构建工具start.spring.io使用)、maven配置(本地maven路径与阿里云镜像设置)以及编码配置(IDEA和eclipse中的编码设置)。通过这些步骤,帮助开发者顺利完成Spring Boot项目的初始化和运行准备。
33 0
微服务——SpringBoot使用归纳——Spring Boot开发环境搭建和项目启动
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
本课主要讲解Spring Boot项目中的属性配置方法。在实际开发中,测试与生产环境的配置往往不同,因此不应将配置信息硬编码在代码中,而应使用配置文件管理,如`application.yml`。例如,在微服务架构下,可通过配置文件设置调用其他服务的地址(如订单服务端口8002),并利用`@Value`注解在代码中读取这些配置值。这种方式使项目更灵活,便于后续修改和维护。
14 0
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录——使用Logger在项目中打印日志
本文介绍了如何在项目中使用Logger打印日志。通过SLF4J和Logback,可设置不同日志级别(如DEBUG、INFO、WARN、ERROR)并支持占位符输出动态信息。示例代码展示了日志在控制器中的应用,说明了日志配置对问题排查的重要性。附课程源码下载链接供实践参考。
33 0
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——指定项目配置文件
在实际项目中,开发环境和生产环境的配置往往不同。为简化配置切换,可通过创建 `application-dev.yml` 和 `application-pro.yml` 分别管理开发与生产环境配置,如设置不同端口(8001/8002)。在 `application.yml` 中使用 `spring.profiles.active` 指定加载的配置文件,实现环境快速切换。本节还介绍了通过配置类读取参数的方法,适用于微服务场景,提升代码可维护性。课程源码可从 [Gitee](https://gitee.com/eson15/springboot_study) 下载。
24 0
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
在微服务架构中,随着业务复杂度增加,项目可能需要调用多个微服务。为避免使用`@Value`注解逐一引入配置的繁琐,可通过定义配置类(如`MicroServiceUrl`)并结合`@ConfigurationProperties`注解实现批量管理。此方法需在配置文件中设置微服务地址(如订单、用户、购物车服务),并通过`@Component`将配置类纳入Spring容器。最后,在Controller中通过`@Resource`注入配置类即可便捷使用,提升代码可维护性。
17 0
Spring Boot 两种部署到服务器的方式
本文介绍了Spring Boot项目的两种部署方式:jar包和war包。Jar包方式使用内置Tomcat,只需配置JDK 1.8及以上环境,通过`nohup java -jar`命令后台运行,并开放服务器端口即可访问。War包则需将项目打包后放入外部Tomcat的webapps目录,修改启动类继承`SpringBootServletInitializer`并调整pom.xml中的打包类型为war,最后启动Tomcat访问应用。两者各有优劣,jar包更简单便捷,而war包适合传统部署场景。需要注意的是,war包部署时,内置Tomcat的端口配置不会生效。
701 17
Spring Boot 两种部署到服务器的方式
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
84 7
【Spring项目】表白墙,留言板项目的实现
本文主要介绍了表白墙项目的实现,包含前端和后端代码,以及测试
【Spring】——SpringBoot项目创建
SpringBoot项目创建,SpringBootApplication启动类,target文件,web服务器,tomcat,访问服务器

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等