SpringBoot 快速入门之IDEA下快速搭建Web项目

目录

SpringBoot 开发可简单分为如下几步:

1、建立新项目(或模块)

2、项目结构

3、编写简易的controller类进行验证

 4、启动SpringBoot引导类


SpringBoot 开发可简单分为如下几步:

  • 创建新模块,选择Spring初始化,并配置模块相关基础信息
  • 选择当前模块需要使用的技术集
  • 开发控制器类
  • 运行自动生成的Application类

1、建立新项目(或模块)

 

2、项目结构

3、编写简易的controller类进行验证

package com.example.controller;

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

@RestController
@RequestMapping("/demos")
public class DemoController {
    @GetMapping("/{id}")
    public String test(@PathVariable Integer id){
        System.out.println("id: "+id);
        return "Hello SpringBoot!";
    }
}

 4、启动SpringBoot引导类

请求:http://localhost:8080/demos/1

 原因:IDEA目录结构的问题,引导类与controller包不属于同一层次,(引导类在demo包下)使得引导类无法加载到controller下的内容。

解决方法:将引导类移入com.example包下,使得与controller包在在同一级目录下。

重启便可以访问。

猜你喜欢

转载自blog.csdn.net/m0_56942812/article/details/125985630