操作大致为:file->new->project->gradle(在左侧选项栏中)
创建常规以后生成的工程目录如下:
下面需要对两个文件进行编辑:
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; } }
如对本文有疑问, 点击进行留言回复!!
荐 嵌入式linux开发环境搭建(Docker版,基于iTop 4412开发板)
SpringBoot项目部署到服务器之 Liunx环境安装jdk详细步骤
网友评论