如何创建spring-boot的web项目

第一步:新建一个maven项目

  1. 新建项目,选择maven


    如何创建spring-boot的web项目_第1张图片
    image.png
  2. 填写GroupId和ArtifactId


    如何创建spring-boot的web项目_第2张图片
    image.png
  3. 下一步默认即可,直接点击finish


    如何创建spring-boot的web项目_第3张图片
    image.png
  4. 创建完成后项目结构如下


    如何创建spring-boot的web项目_第4张图片
    image.png

第二步: 配置pom.xml

在pom.xml中添加如下代码:

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
    
  • parent指定项目的父项目是spring-boot的版本管理中心,因为有了这个,spring-boot项目中导入其他spring的jar包时,就不再需要指定版本号。
  • spring-boot-starter-web 是启动器,帮我们导入了web模块正常运行所需要的所有依赖的组件。

第三步:添加主程序类

新建DemoMainApplication.class:


image.png

内容如下:

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

@SpringBootApplication
public class DemoMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoMainApplication.class,args);
    }
}

第四步:添加controller

在DemoMainApplication.class的所在目录创建controller文件夹并创建DemoController.class


如何创建spring-boot的web项目_第5张图片
image.png

内容如下:

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

@RestController
public class DemoController {
    @RequestMapping("/hello")
    public String Hello(){
        return "Hello World!";
    }
}

第五步:启动

如何创建spring-boot的web项目_第6张图片
image.png

日志出现如下日志说明启动成功:
image.png

浏览器中访问: http://localhost:8080/hello
如何创建spring-boot的web项目_第7张图片
image.png

你可能感兴趣的:(如何创建spring-boot的web项目)