第一个SpringBoot应用
程序员文章站
2022-06-17 16:26:57
...
1.从IDEA中打开 File-New-Project
填写项目信息
选择web依赖
选择mysql依赖
设置项目名称和指定工程目录
生成的项目结构如下图所示:
添加一个Controller测试,包结构如下图所示:
HelloController.java
package com.example.springboot.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 "hello spring boot!";
}
}
运行 com.example.springboot.SpringbootDemoApplication 类的main方法
报错如下:
2018-08-03 17:29:41.811 ERROR 5868 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1
根据错误提示可知是没有配置数据源,因为创建工程的时候我勾选了mysql支持,所以springboot添加了数据源自动配置,但是作为单纯的springboot工程创建演示这里并不需要使用到mysql,所以我们可以通过配置去掉这个数据源的自动配置
解决方法:
修改 SpringbootDemoApplication类上的注解为 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
package com.example.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
再次运行 com.example.springboot.SpringbootDemoApplication 类的main方法,控制台打印日志,最后一行如下,表示启动成功
2018-08-03 17:46:15.294 INFO 2644 --- [ main] c.e.s.SpringbootDemoApplication : Started SpringbootDemoApplication in 1.812 seconds (JVM running for 2.448)
访问测试:http://localhost:8080/hello,浏览器显示如下
至此,第一个springboot应该就构建完毕了
上一篇: 一次上传多个文件
下一篇: [转]Kafka文件存储机制那些事