前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >手把手的SpringBoot教程,SpringBoot创建web项目(三)

手把手的SpringBoot教程,SpringBoot创建web项目(三)

作者头像
剽悍一小兔
发布2018-05-17 15:34:51
7900
发布2018-05-17 15:34:51
举报

这节课,我们来学习一下SpringBoot的环境配置,在SpringBoot中,所有的配置都写在application.properties中:

我们启动项目,默认端口是8080,我们现在给他配置一个8088:

代码语言:javascript
复制
server.port=8088

运行启动类,然后在浏览器地址栏访问上一节中的控制器:

启动成功了。

然后访问这个Controller

代码语言:javascript
复制
package com.example.demo.controller;

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

@RestController
public class HelloController {
    
    @RequestMapping("hello")
    public String hello(){
        return "<font style='font-size:28px;'>Hello Spring Boot</font>";
    }
}

访问:http://localhost:8088/hello

访问成功啦!

如果你要加上一个项目的名字,做如下配置:

代码语言:javascript
复制
server.context-path=/demo

注意:/demo的 “ / ”不能少!

重启服务,访问的地址则需要改为: http://localhost:8088/demo/hello

可见,一样能够成功访问!

接下来,我们再来介绍一种yml配置方式,这里编写一个yml文件,文件名还是application

代码语言:javascript
复制
server:
    port: 8088
    context-path: demo

这样写的好处就是,有一个层级关系,不需要每一行都写全了。需要注意的是,port:8088之间必须要有一个空格,不能挤在一起,否则是识别不了的!!!

启动项目,报错:

代码语言:javascript
复制
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadIntoGroup(ConfigFileApplicationListener.java:476)
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:465)
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:386)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.addPropertySources(ConfigFileApplicationListener.java:225)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:195)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:182)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:168)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:296)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at com.example.demo.DemoApplication.main(DemoApplication.java:10)

注意哦,这里有个坑,我们采用yml文件配置项目的时候,二级配置这里,不能直接一个tab键,那样会有4个空格,而实际上,我们这里只能有两个空格。

一个空格都不能多!!

OK,修改yml文件(老老实实打两个空格吧,亲!):

代码语言:javascript
复制
server:
  port: 8088
  context-path: demo

图解:

重新启动项目,访问: http://localhost:8089/demo/hello

成功!

本篇教程讲解了在SpringBoot项目中进行配置的两种方式,我个人推荐大家使用yml文件来配置SpringBoot项目。

您的支持是我写作的最大动力:


个人网站:http://java520.top/

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.03.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一个空格都不能多!!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档