第一个SpringBoot应用

2 篇文章 0 订阅
1 篇文章 0 订阅

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应该就构建完毕了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值