【发布时间】:2015-09-21 01:43:20
【问题描述】:
我一直在努力争取资源来工作,我知道有成千上万个类似的问题。无论如何,我的条件是这样的
我将从我的 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>
这是我正在处理的错误
在终端中我看到了这个
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
这是什么意思?
我有一个正确的结构并按照惯例去做
但是没有运气。我尝试更改为 public/ 并尝试使用 webjars。一切都是徒劳。这个的规范是什么,我如何设置它,这样它就不会被打破!谢谢!
EDIT:: 配置文件
这是我的 Spring Boot 提供的应用程序文件
@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 文件,但我尝试在 addResourceHandlers 上添加 @Overide,但没有任何改进
@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);
}
}
编辑
在进一步检查应用程序后,我发现了这个
它说它允许的请求是 PUT,但是为什么呢?据我了解,有些东西阻止了我对资源文件的请求,对吗?
急于解决这个问题,配置 Spring 真的很麻烦,即使使用 Spring Boot!
【问题讨论】:
-
你有配置类吗?您的应用程序是否以 root 身份运行?我建议使用 web jars 而不是包含您自己的 bootstrap 版本。尝试在您的资源 URL 前加上
/static前缀,尽管我希望这适用于默认配置。 -
不,我在 Spring Boot 附带的 Application 类上使用 AutoConfiguration 注释。我尝试过使用 webjars,但我不知道如何配置它们。上次我尝试加载它们时遇到错误,它根本无法编译
-
发布你的应用类。为什么它不能用 web jars 编译?您是否包含正确的依赖项?此外,当使用 web jar 时,您显然必须更改资源 url。
-
看起来
<mvc:resources mapping="/resources/**" location="/resources/static/" />配置可能丢失或设置不正确。我建议您在问题中添加更多详细信息。和项目 spring 配置一样, -
是的,看起来就是这样。应用程序的某些部分工作得很好,但资源管理却不行。我应该如何使用 Java 手动配置它?
标签: java spring maven spring-mvc spring-boot