SpringBoot入门案例

SpringBoot整合了Tomcat,让其无需搭载在Tomcat中就可以运行。
该案例搭载热部署:https://blog.youkuaiyun.com/Android_Cob/article/details/105576786
以及解决中文乱码和文件读取:
https://blog.youkuaiyun.com/Android_Cob/article/details/105577513
目录结构
在这里插入图片描述

1.导入坐标(pom)

<?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>
    <!--所有的springboot工程都必须继承spring-boot-starter-parent-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <groupId>edu.xiaoying</groupId>
    <artifactId>SpringBoot_01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--web功能的起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--热部署配置
        **IDE在设置的Build下的Compile勾选Build project automatically,
                    “Ctrl+shift+alt+/ 选Registry 再选compiler.automake.allow.when.app.running” ** -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>


</project>

2.声明启动类(MySpringBootApplication)

package edu.xiao;

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

/*声明这个是一个SpringBoot的引导类*/
@SpringBootApplication
public class MySpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class);
    }
}

3.创建控制器(QuickController)

package edu.xiao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class QuickController {

    @RequestMapping("/firstMethod")
    public @ResponseBody String firstMethod(){
        return "SpringBoot入门成功!";
    }
}

4.创建文件读取的控制器(QuickConfigurationAnnoController)

注意:IDE默认是GBK,可能有中文乱码,上面博客有解决方式

package edu.xiao.controller;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ConfigurationProperties(prefix = "person")
public class QuickConfigurationAnnoController {
    private String name;
    private String address;
    private Integer age;

    @RequestMapping("/ConfigurationMethod")
    public @ResponseBody String firstMethod(){
        return "age:"+age+",name:"+name+",address:"+address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

5.配置文件

注意:当properties与yml文件都有同一个属性时,properties会覆盖yml的属性

如:(application.properties)
#服务器端口
server.port=8088
server.servlet.context-path=/demo
person.age = 18
person.name = 张三
person.address = 北京

(application.yml)
person:
  name: 李四
  age: 22
  address: 湖南

6.运行MySpringBootApplication后的效果

覆盖了yml的属性
在这里插入图片描述
入门效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这次最后一次熬夜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值