Spring boot Gradle项目搭建
程序员文章站
2022-07-04 22:56:28
使用IDEA搭建Java Spring Boot Gradle工程 ......
spring boot gradle项目搭建
使用idea创建gradle工程
操作大致为:file->new->project->gradle(在左侧选项栏中)
创建常规以后生成的工程目录如下:
- build
- gradle
- wrapper
- gradle-wrapper.jar
- gradle-wrapper.properties
- wrapper
- src
- java
- resources
- test
- java
- resources
- build.gradle
- gradlew
- gradlew.bat
- settings.gradle
配置spring boot
下面需要对两个文件进行编辑:
build.gradle文件修改后的内容如下,依赖一般是前面是groupid,中间是artifactid,第三个一般是版本。在repositories配置使用阿里云的仓库,避免下载过慢。
plugins { id 'java' id 'com.gradle.build-scan' version '2.0.2' id 'org.springframework.boot' version '2.0.5.release' id 'io.spring.dependency-management' version '1.0.7.release' } group 'seckill' version '1.0-snapshot' sourcecompatibility = 1.8 repositories { maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } mavencentral() } dependencies { testcompile group: 'junit', name: 'junit', version: '4.12' implementation 'org.springframework.boot:spring-boot-dependencies:2.0.5.release' implementation 'org.springframework.boot:spring-boot-starter-web' testimplementation 'org.springframework.boot:spring-boot-starter-test' components { withmodule('org.springframework:spring-beans') { allvariants { withdependencyconstraints { // need to patch constraints because snakeyaml is an optional dependency it.findall { it.name == 'snakeyaml' }.each { it.version { strictly '1.19' } } } } } } } buildscan { // always accept the terms of service termsofserviceurl = 'https://gradle.com/terms-of-service' termsofserviceagree = 'yes' // always publish a build scan publishalways() }
setting.gradle文件修改后的内容如下:
rootproject.name = 'seckill' enablefeaturepreview('improved_pom_support')
编写类
首先在src/java下生成源码目录com.seckill.spring(相当于com/seckill/spring)
在src/java下创建程序入口类application.java,其内容如下:
package com.seckill.spring; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication public class application { public static void main(string[] args) { springapplication.run(application.class, args); } }
在src/java/com.seckill.spring下创建目录controllers,并在controllers目录创建类:helloworld,在其中定义根路由并返回hello world,其代码如下:
package com.seckill.spring.controllers; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; import java.util.hashmap; import java.util.map; @restcontroller public class helloworld { @requestmapping(value = "/", method = requestmethod.get) public map helloworld() { map<string, object> ret = new hashmap<>(); ret.put("ret", "hello world"); return ret; } }
运行访问
- 运行application类,可以在控制台看到起默认监听在8080端口
- 浏览器访问:localhost:8080,可以看到返回字符串hello world
参考链接
推荐阅读
-
spring boot利用docker构建gradle项目的实现步骤
-
Spring boot搭建web应用集成thymeleaf模板实现登陆
-
spring boot + quartz集群搭建的完整步骤
-
Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)
-
spring boot admin 搭建详解
-
Spring Boot解决项目启动时初始化资源的方法
-
通过Spring Boot + Mybatis + Redis快速搭建现代化Web项目
-
详解快速搭建Spring Boot+Spring MVC
-
Maven工程搭建spring boot+spring mvc+JPA的示例
-
IntelliJ IDEA 创建spring boot 的Hello World 项目(图解)