springboot教程(一) - 项目搭建

本文主要介绍springboot项目搭建的相关demo。

环境依赖

  1. ide:idea;
  2. java版本:jdk1.8;
  3. springboot版本:2.3.9.RELEASE;
  4. maven版本:3.6.3;
  5. 操作系统:windows10 64位。

一、创建项目

  1. 打开idea-file-new-project;

  2. 选择maven项目,点击next按钮;在这里插入图片描述

  3. 设置程序名称、目录以及GroupId及ArtifactId等相关配置,点击finish按钮生成project;在这里插入图片描述

pom.xml

引入springboot依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ichpeng.springboot.demo</groupId>
    <artifactId>spring-boot-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>spring-boot-demo</name>
    <description>springboot初始化样例</description>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

创建测试接口

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

/**
 * @author: ichpeng@qq.com
 * @date: 2021/3/16 16:39
 * @description:
 */
@RestController
public class TestController {
    @RequestMapping("/test")
    public Object testApplication(){
        return "the application is ok!";
    }
}

创建启动程序

创建DemoApplication.java文件,在类加@SpringBootApplication注解,添加main方法

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

/**
 * @author: ichpeng@qq.com
 * @date: 2021/3/16 16:10
 * @description:
 */
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        System.out.println("SpringBoot程序启动。。。");
    }
}

在这里插入图片描述

启动程序

运行main方法即可,默认端口8080。
在这里插入图片描述
访问http://127.0.0.1:8000/test
在这里插入图片描述

配置文件

springboot支持.properties、.yml等格式配置文件(以application.yml为例)

在resources目录下创建application.yml配置文件

  • 修改端口号:
 server:
  port: 8000
  • 定义程序名称:
spring:
  application:
    name: spring-boot-demo

重启查看效果:
在这里插入图片描述

代码结构

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值