如何利用MAVEN创建一个SpringBoot项目

107 阅读1分钟

创建第一个SprinBoot项目

1、创建一个MAVEN项目

image.png

2、打开pom.xml导入spring boot相关依赖

```` spring-boot-starter-parent org.springframework.boot 2.7.2 org.springframework.boot spring-boot-starter-web 2.7.2 ````

3、编写一个主程序:启动SpringBoot应用

image.png

package com.example;

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

/**
 * @SpringBootApplication来标注一个主程序类,说明这是一个SpringBoot应用
 * */
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

4、编写相关的Controller


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        return "第一个SpringBoot程序!";
    }
}

5、启动程序访问http://localhost:8080/hello

image.png