[英]Unable to serve css files placed in src/main/resources/static/css/ folder to client in Spring boot application
[英]Unable to serve static resources with Spring Boot
我有一個具有以下結構的Sprint Boot Gradle應用程序: 項目結構的屏幕快照
項目結構
/build.gradle
/src/main/resources/static/css/default.css
/src/main/java/com/expensalyze/ExpensalyzeApplication.java
/src/main/java/com/expensalyze/web/TagController.java
基本上我的css文件位於src/main/resources/static/css/default.css
。 我希望在http://localhost:8080/css/default.css
處獲取此文件。
根據此鏈接以及spring boot docs , 僅需上述設置。 但是當我嘗試訪問http://localhost:8080/css/default.css
時遇到404錯誤
的build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '2.0.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web:2.0.2.RELEASE')
}
ExpensalyzeApplication.java
@SpringBootApplication
@EnableWebMvc
@ComponentScan(basePackages = "com.expensalyze")
public class ExpensalyzeApplication {
public static void main(String[] args) {
SpringApplication.run(ExpensalyzeApplication.class, args);
}
}
TagController.java
@Controller
public class TagController {
@RequestMapping("/tag") // This works properly
@ResponseBody
String tag() {
return "tag";
}
}
重現此問題的最低限度代碼可在此處的分支static-res-test
。 您可以通過以下步驟運行項目(需要JDK 8):
$ ./gradlew clean build
$ java -jar build/libs/expensalyze.jar
如果您訪問http:// localhost:8080 / tag,則可以使用,但是如果您訪問http:// localhost:8080 / css / default.css ,則找不到它!
我想念什么?
永遠不要在Boot應用程序中使用EnableWebMvc。 它刪除了Spring引導完成的所有MVC自動配置,並由您負責一切 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.