2015-07-03 112 views
1

我一直爭取與得到的資源相當一段時間現在的工作,我知道有成千上萬的類似的問題。不管怎麼樣,我的病情是這樣的無法在春季啓動應用程序訪問和使用靜態資源

我將從我的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> 

<groupId>com.ruruapps</groupId> 
<artifactId>spring-demo</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>jar</packaging> 

<name>spring-demo</name> 
<description>Spring-Demo Application</description> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.2.4.RELEASE</version> 
    <relativePath/> <!-- lookup parent from repository --> 
</parent> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <org.thymeleaf-version>2.1.4</org.thymeleaf-version> 
    <webjars-bootstrap.version>3.3.5</webjars-bootstrap.version> 
    <webjars-jquery-ui.version>2.1.1</webjars-jquery-ui.version> 
    <webjars-jquery.version>2.0.3-1</webjars-jquery.version> 
    <java.version>1.8</java.version> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-actuator</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-batch</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-rest</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-undertow</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-tomcat</artifactId> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <!-- Thymeleaf --> 
    <!--<dependency>--> 
     <!--<groupId>org.thymeleaf</groupId>--> 
     <!--<artifactId>thymeleaf</artifactId>--> 
     <!--<version>2.1.4.RELEASE</version>--> 
    <!--</dependency>--> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 

    <!-- Webjars (static dependencies distributed as WAR files) --> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-websocket</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <scope>runtime</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.webjars</groupId> 
     <artifactId>bootstrap</artifactId> 
     <version>3.3.5</version> 
    </dependency> 
    <dependency> 
     <groupId>org.webjars</groupId> 
     <artifactId>jquery</artifactId> 
     <version>2.1.4</version> 
    </dependency> 
</dependencies> 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</build> 
</project> 

這裏是我處理的

Errors loading resources

並在終端的錯誤,我看到這個

Looking up handler method for path /css/bootstrap/css/bootstrap.min.css 
Did not find handler method for [/css/bootstrap/css/bootstrap.min.css] 
Request method 'GET' not supported 

這是什麼意思?

我受公約

Structure

,但沒有運氣什麼那麼有一個正確的結構,而且做起來。我試圖改用public /並嘗試使用webjars。一切都是徒勞。這是什麼規範,我如何得到這個設置,所以它不會被打破!謝謝!

編輯::配置文件

這裏是提供與彈簧引導

@SpringBootApplication 
@EnableAutoConfiguration(exclude = { 
     org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class, 
     org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration.class}) 
@ComponentScan(basePackages = {"webapp"}) 
@EnableJpaRepositories 
public class SpringDemoApplication { 

    public static void main(String[] args) { 
     ApplicationContext ctx = SpringApplication.run(SpringDemoApplication.class, args); 
    } 
} 

我的應用程序文件,我有一個WebConfig文件,其中大部分是空的,但我想加入@Overide上addResourceHandlers,沒有改進

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = {"webapp"}) 
public class WebConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/static/").setCachePeriod(31556926); 
    } 
} 

編輯

進一步審查申請後,我發現這

Trace output

它說,它允許請求PUT,但爲什麼呢?從我可以理解的是,有些東西阻止了我對資源文件的請求,是嗎?

不顧一切地搞定這個問題,即使使用Spring Boot,也很難配置Spring!

+0

你有配置類嗎?你的應用程序是以root身份運行的嗎?我會建議使用網絡瓶子,而不是包括你自己的bootstrap版本。嘗試用'/ static'爲你的資源網址添加前綴,但我希望這可以使用默認配置。 –

+0

不,我在Spring Boot附帶的Application類上使用AutoConfiguration註冊。 我嘗試過使用webjars,但我不知道如何配置它們。上次我嘗試加載它們時出錯,它根本無法編譯 –

+0

發佈您的應用程序類。爲什麼不用Web Jar進行編譯?你有沒有包含正確的依賴關係?另外,當使用網絡瓶,你將不得不改變資源網址顯然。 –

回答

4

發現我面臨的問題是無法訪問靜態資源。 對於那些經歷相同的人:

此行爲(405錯誤)表示某事正在阻止您的靜態資源的路徑。這不必是/靜態的或/公共的。它不會做任何事情。

在我的情況下,問題是控制器阻止/ css路徑。要解決此問題,需要在類lvl上註釋一個@RequestMapping(value = "/Somepath")註釋,以釋放靜態資源的路徑。

這爲我工作,現在我可以訪問我的靜態資源沒有任何問題。

這可能是一個非常煩人的問題,有人可以花幾個小時和幾天的時間來解決這個問題。希望這可以幫助其他人如果有防止這種行爲,請在評論中提到

0

如果你想通過配置類來配置資源映射,你在評論中提及,那麼就應該是這樣的:

@EnableWebMvc 
@ComponentScan(basePackages = {"org.company.yourpackage"}) 
@Configuration 
public class AppConfig extends WebMvcConfigurerAdapter { 
    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/static/").setCachePeriod(31556926); 
    } 
} 

this後也可能會有幫助。

+0

這也沒有做任何事情。我已經更新了這個問題。它可能會提供更多線索 –

0

我有完全相同的問題,是什麼幫助我被添加的替代或其他方法,以節省他們的時間

enter code here 
<servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>*.js</url-pattern> 
</servlet-mapping> 

OR

<servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>/js/*</url-pattern> 
</servlet-mapping> 

這個職位 servlet for serving static content here

相關問題