Spring boot Gradle项目搭建


使用IDEA创建Gradle工程

    操作大致为:File->new->Project->Gradle(在左侧选项栏中)

    创建常规以后生成的工程目录如下:

  • build
  • gradle
    • wrapper

      • gradle-wrapper.jar
      • gradle-wrapper.properties
  • 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 Gradle项目搭建的更多相关文章

  1. SpringCloud 微服务一:spring boot 基础项目搭建

    spring cloud是建立在spring boot的基础上的,而之前虽然听说过,也随便看了一下spring boot,却没有真正使用,因此还必须先花时间学一下spring boot. spring ...

  2. 【Spring boot】【gradle】idea新建spring boot+gradle项目

    在此之前,安装了idea/jdk/gradle在本地 ===================================== gradle怎么安装:http://www.cnblogs.com/s ...

  3. Spring Boot gradle

    最近有写一个电子订单商务网站,使用JAVA8,SPRING,ANGULARJS对项目使用的技术和大家分享. 第一次写博客,哪有不对需要改正的请联系改正. 因为是项目是我给别人做的无法提供源码见谅,我尽 ...

  4. 使用intelliJ创建 spring boot + gradle + mybatis站点

    Spring boot作为快速入门是不错的选择,现在似乎没有看到大家写过spring boot + gradle + mybatis在intellij下的入门文章,碰巧.Net同事问到,我想我也可以写 ...

  5. spring boot + gradle + mybatis

    使用intelliJ创建 spring boot + gradle + mybatis站点   Spring boot作为快速入门是不错的选择,现在似乎没有看到大家写过spring boot + gr ...

  6. 15 个优秀开源的 Spring Boot 学习项目,一网打尽!

    Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 8 次,Spring Boot 的受 ...

  7. 【spring】1.2、Spring Boot创建项目

    Spring Boot创建项目 在1.1中,我们通过"Spring Starter Project"来创建了一个项目,实际上是使用了Pivotal团队提供的全新框架Spring B ...

  8. 15 个优秀开源的 Spring Boot 学习项目

    Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 8 次,Spring Boot 的受 ...

  9. spring boot 开发环境搭建(Eclipse)

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

随机推荐

  1. getSuperclass与getGenericSuperclass区别

    声明三个类class Person<T, V> {}class Teacher {}class Student extends Person<Student, Teacher> ...

  2. 粗暴,干就完了----徐晓冬似的C语言自学笔记----前言

    10对年前就觉得C/C++语言很酷,第一印象就是90年代末,个人电脑在中华大地开始普及的岁月中,层出不穷的病毒,对了,全是C/C++写的:除了危及人民群众信息安全以外,C系列语言用途甚广,可以发明其他 ...

  3. spring-boot-configuration-processor 是干啥用的

    spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,就需要使用spring-boot-configuration-processor了 引入pom依赖 <de ...

  4. 【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps

    题目如下: You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 positio ...

  5. 图的最小生成树(java实现)

    1.图的最小生成树(贪心算法) 我两个算法的输出都是数组表示的,当前的索引值和当前索引对应的数据就是通路,比如parent[2] = 5;即2和5之间有一个通路,第二个可能比较好理解,第一个有点混乱 ...

  6. HGOI 20191107 题解

    Problem A 树状数组 给出下列$C++$代码: 设区间加操作$modify(l,r)$为调用两次$update(r,1)$和$update(l-1,-1)$ 设$f(l,r)$表示在初始$cn ...

  7. ros中同时订阅两个topic(2张图像)合并成一个topic(1张图像)

    2019-12-06 15:42:39 先暂时做个资料保存 要同时用两个红外相机,但是没有做硬件上的 时间戳同步,就是笔记本上同时插着两个相机. 两个topic发布各自相机的图像,然后要有个节点同时订 ...

  8. 7.20T1

    排序(sort) [问题描述] 有 n 个人依次站在小 A 面前.小 A 会依次对这 n 个人进行 m 次操作. 每次操作选择一个位置 k,将这 n 个人中的所有身高小于等于当前 k 位置的 人的身高 ...

  9. 说说如何使用unity Vs来进行断点调试

    转载自:http://dong2008hong.blog.163.com/blog/static/4696882720140293549365/ 大家可以从这下载最新版的unity vs. Unity ...

  10. C#_选择结构,Console的应用,数据类型转换

    1:先看一个顺序结构编程,代码如下: using System; using System.Collections.Generic; using System.Linq; using System.T ...