SpringBoot--只是入门

SpringBoot简化了很多的配置,让开发人员只关注开发即可,所以使用的人数很多。

1.先创建工程后,在pom.xml中添加依赖。为了方便管理依赖,直接添加parent模块进行依赖版本的管理。

 1    <parent>//管理依赖
 2         <groupId>org.springframework.boot</groupId>
 3         <artifactId>spring-boot-starter-parent</artifactId>
 4         <version>2.1.5.RELEASE</version>
 5     </parent>
 6 
 7 //随后添加依赖
 8 
 9     <dependencies>
10         <dependency>
11             <groupId>org.springframework.boot</groupId>
12             <artifactId>spring-boot-starter-web</artifactId>
13         </dependency>
14     </dependencies>

也可以添加对JDK版本的管理

<properties>
            <java.version>1.8</java.version>
    </properties>

2.添加依赖后在java文件夹内添加SpringBoot启动项Application(注意一定要添加在需要启动的项目外部,不可以与项目同一个文件夹层)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


//springboot工程都有一个启动引导类   这是一个工程的入口类
@SpringBootApplication  
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

3创建子文件夹添加测试类

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

@RestController
public class Hello {

    @GetMapping("hello")//getmapping或requestMapping都可以
    public String hello(){
        return "hello world";
    }
}

4.完成编写后启动Application,然后浏览器输入localhost:8080/hello 就可以查看是否成功*(因为Spring-boot-starter-web 的默认服务器端口是8080)

原文地址:https://www.cnblogs.com/j9527/p/13019560.html