当前位置: 移动技术网 > IT编程>开发语言>Java > Spring boot Gradle项目搭建

Spring boot Gradle项目搭建

2019年08月01日  | 移动技术网IT编程  | 我要评论

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

参考链接

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网