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. MyBatis-09-Lombok

    9.Lombok Project Lombok is a java library that automatically plugs into your editor and build tools, ...

  2. Path variable [contentHash:8] not implemented in this context: styles.[contentHash:8].css

    webPack 升级到 4.3.0 导致 extract-text-webpack-plugin 无法使用  

  3. loj515 「LibreOJ β Round #2」贪心只能过样例[bitset+bool背包]

    由于bitset极其不熟练且在实际题目中想不起来运用它来优化,于是练了几道题. 这题是一个分组的bool背包,每组必须选一个,暴力的话是$O(n^5)$. 如果dp数组不要一维滚动的话,有两种枚举方法 ...

  4. docker零碎知识

    一.关于容器的时区配置: docker中如果对时区不加限制,默认会采用GMT时间,对于东八区来说,需要修改,修改方式有多种: 1.在Dockerfile中的修改 FROM centos: MAINTA ...

  5. JS 定时器-setInterval、clearInterval、setTimeout

    在微信小程序里写的: // pages/splash/splash.js const app = getApp() Page({ data: { remainSecond: }, // 页面渲染完成后 ...

  6. Java进阶知识09 Hibernate一对多单向关联(Annotation+XML实现)

    1.Annotation 注解版 1.1.在一的一方加Set 1.2.创建Customer类和Order类 package com.shore.model; import java.util.Hash ...

  7. 数据结构实验之链表九:双向链表(SDUT 2054)

    #include <bits/stdc++.h> using namespace std; typedef struct node { int data; struct node *nex ...

  8. JavaWeb_(Spring框架)SpringAOP面向切面编程

    SpringAOP:面向切面编程(面向fifter编程) 通俗易懂术语:所有纵向重复的代码,我们提取成横向的代码 以下文章内容参考知乎:从0带你学习SpringAOP,彻底的理解AOP思想 传送门 1 ...

  9. 2017 ZSTU寒假排位赛 #8

    题目链接:https://vjudge.net/contest/149845#overview. A题,水题. B题,给出 p个 第一个人的区间 和 q个第二个人的区间,问[l,r]中有多少个整数满足 ...

  10. [CTS2019]珍珠——二项式反演

    [CTS2019]珍珠 考虑实际上,统计多少种染色方案,使得出现次数为奇数的颜色数<=n-2*m 其实看起来很像生成函数了 n很大?感觉生成函数会比较整齐,考虑生成函数能否把n放到数值的位置,而 ...