在我们继续使用 Gradle 创建我们的演示 Spring Boot Web 应用程序之前,我假设我们都已准备好 Gradle 设置。
现在我们已经准备好安装插件,创建一个新的 Gradle 项目,如下所示 –
单击下一步,指定项目详细信息,如下所述 -
点击完成,我们就完成了初始项目的创建——
就像我们有 pom.xml 和 Maven 一样,我们有 build.gradle 和 Gradle。让我们通过添加 Spring Boot 依赖项对其进行所需的更改 -
buildscript { ext { springBootVersion = '1.4.3.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' jar { baseName = 'boot-gradle' version = '0.0.1-SNAPSHOT' } repositories { mavenCentral() } bootRepackage { enabled = true } dependencies { compile('org.springframework.boot:spring-boot-starter', 'org.springframework.boot:spring-boot-starter-web', 'org.springframework.boot:spring-boot-starter-thymeleaf') }
现在让我们创建包含主要方法的 SpringBootApplication 类 –
SpringBootApplication.java
package com.jcombat; import org.springframework.boot.SpringApplication; @org.springframework.boot.autoconfigure.SpringBootApplication public class SpringBootApplication { public static void main(String[] args) { SpringApplication.run(SpringBootApplication.class, args); } }
同样,让我们也创建一个 Spring 控制器类 –
DemoController.java
package com.jcombat.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class DemoController { @RequestMapping("/") public String welcome(Map<String, Object> model) { return "welcome"; } }
我们现在需要在 Spring Boot 资源位置添加 welcome.html。请注意,在创建 Gradle 项目时资源文件夹可能不存在,因此在我的例子中,我必须明确创建一个。
项目结构现在应该是这样的——
现在在src/main/resource 项目目录中创建“template”文件夹,并在其中创建welcome.html 文件。请参阅下面的快照 -
接下来,右键单击该项目,转到“Gradle”并单击“刷新 Gradle 项目”。
就是这个。
右键单击该项目并将应用程序作为 Spring Boot App 运行 -
下载源码
标签2: Java教程地址:https://www.cundage.com/article/jcg-spring-boot-web-application-gradle.html