第一个springboot项目报错内容
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Mar 08 11:29:50 CST 2020
There was an unexpected error (type=Not Found, status=404).
No message available
正确的解决方法(最简单的入门demo)
1.打开你的Chapter1Main
2.找到下图这么一行内容
3.把双引号里的内容加到你的地址上(我的就是/test),如下图:
package com.springboot.chapter1;
@Controller
@EnalbeAutoConfiguration
public class Chapter1Main{
@RequestMapping("/test")
@ResponseBody
public Map<String,String> test(){
Map<String,String> map=new HashMap<String,String>();
map.put("key","value");
return map;
}
public static void main (String[] args) throws Exception{
SpringApplication.run(Chapter1Main.class,args);
}
}
事实上,Spring Boot的参数配置除了使用properties文件之外,还可以使用yml文件等,它会以下列的优先级顺序进行加载:
•命令行参数;
•来自java:comp/env的JNDI属性;
•Java系统属性(System.getProperties());
•操作系统环境变量;
•RandomValuePropertySource配置的random.*属性值;
•jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件;
•jar包内部的application-{profile}.properties或application.ym(带spring.profile)配置文件;
•jar包外部的application.properties或application.yml(不带spring.profile)配置文件;
•jar包内部的application.properties或application.ym(不带spring.profile)配置文件;
•@Configuration注解类上的@PropertySource;
•通过SpringApplication.setDefaultProperties指定的默认属性。
实际上,yml文件的配置与properties文件只是简写和缩进的差别,因此差异并不大,所以本书统一使用properties文件进行配置。对于需要使用yml文件的读者,只是需要稍加改动即可。
yml文件
server:
port: 8080
tomcat:
uri-encoding: UTF-8
max-connections: 2000
max-threads: 200
spring:
datasource:
name: local-mysql
url: jdbc:mysql://你的数据库?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: 你的账户
password: 你的密码
driver-class-name: com.mysql.cj.jdbc.Driver
application.properties文件區別
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=user
spring.datasource.password=ThePassword
自己的application.properties配置文件
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis_jorya?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
jdbc.username=root
jdbc.password=1234