springboot (一) helloworld

准备:

eclipse 首先创建一个maven项目。这里不介绍。可以百度。

idea会更简单一些。

 

开搞:

1. 引入相关依赖:https://mvnrepository.com (可以上这个网站上搜索)

			<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter</artifactId>
				<version>2.1.3.RELEASE</version>
			</dependency>

			<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-web</artifactId>
				<version>2.1.3.RELEASE</version>
			</dependency>
			<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-test</artifactId>
				<version>2.1.3.RELEASE</version>
				<scope>test</scope>
			</dependency>

说明一下: 其中spring-boot-starter 是spring-boot 自启动的核心。 他依赖于 spring-boot 。 spring-boot-starter-web 依赖于spring-boot-starter 所以有些文章只写一个 spring-boot-starter-web 也能成功。

 

2. 编写启动类

package com.springboot.mvc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * Hello world!
 *
 */
@SpringBootApplication
//扫描包需要自行制定修改。否则会404
@ComponentScan("com.springboot.mvc")
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class);
	}
}

 

3.编写控制层

package com.springboot.mvc.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class TestController {
	
	private Logger logger = LoggerFactory.getLogger(TestController.class);

	@RequestMapping("check")
	public String checkAlive(){
		logger.info("you are right");
		return "hello world";
	}
}

 

4.启动项目

 

启动项第一个和第二个都是一个意思。

 

5.check 

http://localhost:8080/test/check 

 

可能出现的问题:

1.maven 依赖下不来, maven setting.xml 中添加入阿里云镜像

  <mirrors>
    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>
  </mirrors>

2.maven 强烈建议自己下载一个不要用eclipse自带的。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值