【Spring Boot 基础】快速构建项目,在浏览器和后台显示输出结果

一、代码演示及操作步骤

1、创建Web项目

2、项目结构

在与Application同级目录下创建Controller。重点关注pom、Application、Controller三个文件。

3、代码练习

3.1在浏览器输出Hello World

1)Controller

package com.lc.springboot_testbilbil;

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

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!";
    }
}

2)Application

package com.lc.springboot_testbilbil;

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

@SpringBootApplication
public class SpringBootTestBilBilApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootTestBilBilApplication.class, args);
    }

}

3)运行结果:

后台:

 根据后台提示的端口8080,浏览器地址输入localhost:8080/hello可以得到代码输出结果:

3.2 YAML数据格式和数据获取

配置文件在resources根目录下。在同一级目录下优先级从高到低为:properties>yml>yaml。

1)创建yml文件

 2).yml

 server:
   port: 8081

 name : XiaoLi
#对象
 person1:
   name1 : ${name}
  #XiaoLi
   age1 : 20

#对象行内写法
 person2 : {name2: XiaoYue, age2: 30}

#数组
 address1 :
   -Nanning
   -Liuzhou

  #数组行内写法
 address2 : [Nanning,Liuzhou]

#纯量
 msg1 : 'hello \n world'
#不会识别转义字符,导致原样输出
 msg2 : "hello \n world"
#会识别转义字符

3)Controller

package com.lc.springboot_testbilbil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Value("${name}")
    private String Name;

    @Value("${person1.name1}")
    private String Name1;

    @Autowired
    private Environment env;

    @RequestMapping("/helloYML")
    public String hello(){
        System.out.println(Name);
        System.out.println(Name1);
        System.out.println("person2.name是"+env.getProperty("person2.name2"));
        System.out.println("person2.age是"+env.getProperty("person2.age2"));
        System.out.println("person1.name是"+env.getProperty("person1.name1"));
        System.out.println("person1.age是"+env.getProperty("person1.age1"));
        return "Hello World! 你好林月明!";
    }
}

4)Application

package com.lc.springboot_testbilbil;

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

@SpringBootApplication
public class SpringBootTestBilBilApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootTestBilBilApplication.class, args);
    }

}

5)运行结果

 注:刷新浏览器后,后台才会出现输出结果

二、参考链接

1、黑马程序员SpringBoot教程https://www.bilibili.com/video/BV1Lq4y1J77x?p=5&vd_source=841fee104972680a6cac4dbdbf144b50

2、报错解决

1)报错expected <block end>, but found BlockMappingStart......

解决方法:每个配置行前需要有空格!!!每个“:”两边需要有空格!!!数组中间加空格!!!

参考链接:

https://www.cnblogs.com/chenkeyu/p/6858342.html

2)报错:Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.

解决方法:由于自己粗心,写错了引用字段,改正后就对了

其他参考链接:https://blog.csdn.net/m0_50762431/article/details/122143601

3、Spring配置参考https://docs.spring.io/spring-boot/docs/3.0.5/reference/html/configuration-metadata.html#appendix.configuration-metadata.annotation-processor

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot是一个Java的开源框架,它提供了一个快速构建基于Spring的应用程序的方式。它简化了Spring应用程序的开发,并且集成了大量的其他Spring项目Spring Boot后台管理源码是Spring Boot框架的后台管理系统的源代码。这个后台管理系统可以用于管理和监控Spring Boot应用程序的运行情况,包括配置管理、日志跟踪、错误处理、性能监控等功能。 Spring Boot后台管理源码的核心部分包括一个基于Spring框架的Web应用程序,它可以通过浏览器访问,并且提供了用户界面来管理Spring Boot应用程序。这个后台管理系统通常包括多个模块,比如用户管理、权限管理、日志管理、监控管理等模块,每个模块都有对应的源代码和配置。 在Spring Boot后台管理源码中,可以看到大量的Java代码、HTML模板、样式表、JavaScript代码等。这些源代码主要负责实现后台管理系统的功能,比如通过Java代码来处理用户请求,调用Spring Boot应用程序的接口来获取数据,通过HTML模板和JavaScript代码来实现用户界面等。 除了源代码之外,还有一些配置文件和资源文件,比如Spring Boot应用程序的配置文件、日志文件、静态资源文件等。这些文件也是后台管理系统的重要组成部分,它们的内容会影响后台管理系统的运行和表现。 总的来说,Spring Boot后台管理源码是实现Spring Boot框架后台管理系统的关键部分,它包括了多个模块的源代码、配置文件和资源文件,用于管理和监控Spring Boot应用程序的运行情况。通过研究和理解这些源码,可以帮助开发者更好地理解Spring Boot框架的实现原理,并且可以为自己的项目开发提供参考和借鉴。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林月明

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

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

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

打赏作者

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

抵扣说明:

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

余额充值