springboot的简单搭建(一)

https://blog.csdn.net/qq_41574321/article/details/79352077

项目我去搭建一个。

先贴出整个SpringBoot项目的结构 
这里写图片描述

1.首先新建一个web项目 
2.修改pom.xml配置文件,引入SpringBoot的核心依赖

<dependencies>
        <!--SpringBoot核心模块,包括自动配置支持、日志和YAML-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--测试模块,包括JUnit、Hamcrest、Mockito-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--Web模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3.新建一个启动类,SpringBoot项目都有一个启动类,运行SpringBoot项目时,直接运行启动类即可(启动类好像只能放在java目录下,最外层包中,如我的启动类放在com.example.demo包中)

/**
 * SpringBoot项目启动类
 */
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        //启动时关闭Banner
//        SpringApplicationBuilder builder = new SpringApplicationBuilder(DemoApplication.class);
//        builder.bannerMode(Banner.Mode.OFF).run(args);

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4.在resources目录下新建一个application.yml或application.properties配置文件(.yml和.properties都可以,看个人喜好,了解yml语法之后个人偏向于使用yml)

#服务配置信息
server:
  address: localhost
  context-path: /lilu
  port: 8080
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

访问地址:http://localhost:8080/lilu

基本搭建已经完成,接下来可以写自己的请求接口

@RestController
public class HelloController {
    @Value("${person.name}")
    private String name;

    @RequestMapping("/hello")
    public String hello(){
        System.out.println(name);
        return "hello"+name;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

这里@Value中的${person.name}是我在yml配置文件中配置过的

#服务配置信息
server:
  address: localhost
  context-path: /lilu
  port: 8080
  tomcat:
    uri-encoding: utf-8
person:
 name: 藜芦
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

完成之后,运行 启动类,访问:http://localhost:8080/lilu/hello 即可

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值