背景:使用springboot+vue構建的微信點餐系統
最近在做一個項目, 配置完信息以後,一直報錯, 訪問URL報錯信息如下:
項目的配置信息,pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.imooc</groupId>
<artifactId>sell</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sell</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>repository_ali.mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>repository_ali.org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
注意此項目引入的Spring Boot 版本是2.1.5的,然後項目的配置文件信息如下:
我項目的application.yml配置信息如下:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://192.168.31.204/sell?characterEncoding=utf-8&useSSL=false
jpa:
show-sql: true
server:
context-path: /sell
上網查了很多,原因是SpringBoot啓動類(main方法所在的類)未放在根目錄下,導致@SpringBootApplication掃描不全所有包。
解決方案:也是官網上建議的:將SpringBoot啓動類(main方法所在的類)即Application.java放置在和Controller同級目錄,如下圖所示。
一、controller類必須是Application的所在包的類或者子包的類。
官網那邊說明是,程序只加載Application.java所在包及其子包下的內容。
二、靜態資源的配置
springboot 在未配置訪問靜態資源的情況下,會默認到 templates 文件夾下找index頁面。。我現在還沒有配置,後續會繼續編寫。
而我所遇到的還不是因爲這個原因,因爲我的包目錄結構是正確的。我的項目出現WhitePage的原因竟然是spring-boot配置文件(application.yml)中server.context-path不起作用。不起作用的原因也已經查明:server.context-path的配置方式是SpringBoot 1.x.x版本中出現的,而我引用是2.x.x版本的,需要更改爲server: servlet: context-path: /sell,/sell是訪問的URL中用到的,以後再詳細說明。