springboot - gradle - helloworld

使用gradle  来搭建 springboot 项目简单写个demo

首先下载 gradle  下载最新版就可以了 ,这里是6.1.1 , 

————————————————

然后在初始化springboot项目:https://start.spring.io/

Group: com.leon
 Artifact: leonBlog

依赖选spring web full stark;

下载好以后 看目录例有个 gradlew 和gradlew.bat 分别是给 mac 和window用的 初始化的工具 

在build.gradle修改仓库地址为阿里云的

plugins {
	id 'org.springframework.boot' version '2.2.4.RELEASE'
	id 'io.spring.dependency-management' version '1.0.9.RELEASE'
	id 'java'
}

group = 'com.leon'
version = '1.1.0'
sourceCompatibility = '1.8'

repositories {
	// mavenCentral() 注释掉 运来的仓库,换成阿里云的仓库提速
	maven {
		url 'http://maven.aliyun.com/nexus/content/groups/public'
	}
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation('org.springframework.boot:spring-boot-starter-test') {
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}
}

test {
	useJUnitPlatform()
}

修改gradle.wapper里的版本为最新版

#Sun Feb 09 18:49:52 CST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

——————————————

!安装gradle低版本的不一定能兼容java版本,的安装高版本的来兼容

 执行 gradle -v 查看版本
 执行 gradle build 来构建项目

build 好之后

启动命令 java -jar build/libs/demo-0.0.1-SNAPSHOT.jar 

这样就初始化好了一个项目,开始准备写helloworld 

*************************************

首先写个控制器

package com.leon.contronller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author  wangrong
 * @since 2020.2.9
 *
 * */

@RestController // contronller层
public class HelloContronller {

    @RequestMapping("/hello") // 定义 url
    public String hello(){
        return "helloworld";
    }

}

然后写个测试

package com.leon.contronllerTest;

import com.leon.blog.Application;
import com.leon.contronller.HelloContronller;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static  org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static  org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static  org.hamcrest.Matchers.equalTo;
@SpringBootTest
@AutoConfigureMockMvc // 自动加载
@ContextConfiguration(classes = HelloContronller.class) // 需要吧测试的类 放进去
class HelloContronllerTest {
	// 自动装配
	@Autowired
	private MockMvc mockMvc;
	@Test
	void testHello() throws Exception {
		mockMvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
				.andExpect(status().isOk()) // 状态是否是200
				.andExpect(content().string(equalTo("helloworld"))); // 状态是否是200 返回字段是否为helloworld
	}

}

测试跑通后 就 启动jar包, 看http://localhost:8080/hello 是否能够看到代码

启动有2种方法 1, ide 启动  2, java -jar build/libs/demo-0.0.1-SNAPSHOT.jar   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值