01_SpringBoot简单搭建入门程序

发布于:2024-05-09 ⋅ 阅读:(32) ⋅ 点赞:(0)

1、先创建一个java项目

我的项目结构
在这里插入图片描述

2、导入依赖

<dependencies>
    <!--spring-boot-starter起步依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!--spring-boot-starter-web (spring-webmvc + tomcat) web起步依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--单元测试-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3、将Java项目修改为SpringBoot项目

//2、添加@SpringBootApplication注解
@SpringBootApplication
public class Application
{
    public static void main( String[] args )
    {
    	//1、修改这里
        SpringApplication.run(Application.class, args);
    }
}

4、编写一个测试的Controller

@RestController
@RequestMapping("/books")
public class BookController {
    //访问地址 http://localhost:8080/books/1
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println("id ==> " + id);
        return "hello , spring boot";
    }
}

5、测试(创建一个*.http的文件)

方式1:

### SpringBoot测试
GET http://localhost:8080/books/1
Accept: application/json

测试效果如下
在这里插入图片描述
在这里插入图片描述

方式2:可以直接在浏览器访问该地址

http://localhost:8080/books/1

方式3:使用postman也可以

方式2和方式3比较简单,效果图就不放了
我这里用的idea集成的一个Http插件,我感觉比postman好用,不用再单独打开一个postman软件了,节省电脑内存,直接在idea里就可以测试。


网站公告

今日签到

点亮在社区的每一天
去签到