springboot (一) helloworld

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011377803/article/details/90139168

准备:

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自带的。

猜你喜欢

转载自blog.csdn.net/u011377803/article/details/90139168
今日推荐