二. Spring Boot 入门demo - summerhll/Spring-Boot-demo GitHub Wiki

一. 禁止项目中的数据库配置。

因为项目只是一个小demo, 不需要使用数据库。 然而springboot在启动时会自动注入数据源,如果没有配置的话,会报异常。 所以为了禁止使用数据库,需要增加下面代码:

@SpringBootApplication**(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,HibernateJpaAutoConfiguration.class})** public class DemoApplication {

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

} 参考网址: http://blog.csdn.net/qq_15071263/article/details/78458890

二.创建控制器

  1. 不创建控制器会报错 如果不创建控制器,直接输入 http://localhost:8080/ 访问的话,会报错。 报错信息为: Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Feb 27 11:33:17 CST 2018 There was an unexpected error (type=Not Found, status=404). No message available

  1. 控制器代码 package com.example.demo;

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

/**

  • Created by huangli on 2018/2/27. */ @RestController public class HellowController { @RequestMapping("hello") public String say(){ return "hello Java!"; } }

访问地址: http://localhost:8080/hello 页面效果: