springboot 入门一

本文介绍了如何在Eclipse中安装STS插件,并通过STS创建一个SpringBoot的Demo项目。在遇到新建项目找不到Spring相关的问题时,解决方案是将JDK版本更改为1.8。创建HelloController并设置目录结构,启动主程序后,可以通过http://localhost:8080/test/hello访问测试,同时提供了编写单元测试用例的指导。
摘要由CSDN通过智能技术生成

一、eclipse 安装sts插件

    Help-->Eclipse MraketPlace


二、安装完成新建一个demo 项目

    

注意:如果安装没有问题但是新建项目的时候找不到spring相关,只需要将jdk改成1.8版本的就可以了。

新建项目完成后创建一个helloController,注意目录结构


HelloController

package com.example.demo.controller;

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

@RestController
@RequestMapping("/test")
public class HelloController {
	
	@RequestMapping("/hello")
	public String hello() {
		return "hello,world!";
	}
	
}

运行主程序,访问http://localhost:8080/test/hello 返回 



编写单元测试用例


package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.example.demo.controller.HelloController;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
@WebAppConfiguration
public class DemoApplicationTests {
	@Autowired
	HelloController helloController;

	@Test
	public void getHello() throws Exception {
		System.out.println(helloController.hello());
	}

}
在控制台查看结果即可。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值