Spring boot Gradle project construction
Creating Gradle Project with IDEA
File - > New - > Project - > Gradle (in the left option bar)
The following catalogues are generated after the establishment of the routine project:
- build
- gradle
- wrapper
- gradle-wrapper.jar
- gradle-wrapper.properties
- wrapper
- src
- java
- resources
- test
- java
- resources
- build.gradle
- gradlew
- gradlew.bat
- settings.gradle
Configure Spring boot
The following two documents need to be edited:
The revised version of the_build.gradle file is as follows:
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 { 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() }
The revised_setting.gradle file is as follows:
rootProject.name = 'seckill' enableFeaturePreview('IMPROVED_POM_SUPPORT')
Writing classes
First, the source directory com. seckill. spring (equivalent to com/seckill/spring) is generated under src/java.
Create Application.java under src/java, which is as follows:
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); } }
Create directory controllers under src/java/com.seckill.spring and create a class in the controllers directory: HelloWorld, which defines the root route and returns to HelloWorld. The code is as follows:
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; } }
Running access
- Running the Application class, you can see the default listening on port 8080 in the console
- Browser access: localhost:8080, you can see the return string Hello World