我正在開發一個reactjs項目,我只是做了一個紗線生成並將內容移入了Java項目。Java Spring Boot - 用於訪問靜態文件夾的路由
我的項目\完整的\ src \主\資源\靜態
但是當我查看從Java網站上的項目 - 本地主機:8080 - 我得到一噸的404錯誤
logo.a67f8998.png:1 GET http://localhost:8080/static/media/logo.a67f8998.png
registerServiceWorker.js:77 GET http://localhost:8080/service-worker.js 404
-beefeater.78e4414b.jpg:1 GET http://localhost:8080/static/media/-beefeater.78e4414b.jpg 404()
的
對於這個項目我主要是作爲一個前端開發人員 - 我在哪裏進行必要的更改以使這些文件的路由正確?
我的Application.java文件看起來像這樣 - 當我取消註釋代碼時,我被告知刪除@overide?
//import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
/**
* Hello world!
*
*/
@SpringBootApplication
public class Application implements CommandLineRunner {
//@Autowired
//private AccountRepository accountRepository;
/**
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... arg0) throws Exception {
}
/*
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}*/
}
我試着添加一個配置文件來排序這個..我需要導入它在application.java?
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//@Configuration
@EnableWebMvc
@WebAppConfiguration
public class Configuration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}
}
由於您使用的是'@ SpringBootApplication'沒有必要配置資源處理程序,因爲春天開機時會爲你做的。只要確保static conent位於src/main/resources/static下,例如'src/main/resources/static/myscript.js',那麼如果你需要使用'localhost:8080/myscript.js'並確保刪除'@ EnableWebMvc'。 –
我不明白 - 它位於這裏「\ src \ main \ resources \ static」 - 但我得到404的 - 你說我不需要配置它 - 但爲什麼我得到404的 - 「http: //localhost:8080/service-worker.js 404「 –
'@ EnableWebMvc'將禁用靜態Web內容的彈簧引導自動配置,如果您使用'@ EnableWebMvc',那麼您將被迫配置資源處理程序。 –