Spring boot Gradle項目搭建

Spring boot Gradle項目搭建


使用IDEA建立Gradle工程

    操做大體爲:File->new->Project->Gradle(在左側選項欄中)java

    建立常規之後生成的工程目錄以下:web

  • build
  • gradle
    • wrapper
      • gradle-wrapper.jar
      • gradle-wrapper.properties
  • src
    • java
    • resources
  • test
    • java
    • resources
  • build.gradle
  • gradlew
  • gradlew.bat
  • settings.gradle

配置Spring boot

    下面須要對兩個文件進行編輯:spring

    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文件修改後的內容以下:bash

rootProject.name = 'seckill'
enableFeaturePreview('IMPROVED_POM_SUPPORT')

編寫類

    首先在src/java下生成源碼目錄com.seckill.spring(至關於com/seckill/spring)app

    在src/java下建立程序入口類Application.java,其內容以下:maven

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,其代碼以下:ide

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

參考連接

相關文章
相關標籤/搜索