我创建了一个spring项目,但无法访问静态资源
应用程序
package com.example.demo;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application10_4 extends ResourceConfig{
public Application10_4(){
register(new BookService10_4());
}
public static void main(String[] args){
SpringApplication.run(Application10_4.class, args);
}
}pom.xml中的依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>当我使用自生成应用程序时,我可以访问静态资源。
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}发布于 2022-01-16 19:52:53
register(new BookService10_4());是什么?
Spring将自动添加位于下列任意目录中的静态web资源:
/META-INF/resources/
/resources/
/static/
/public/因此,您可以在public/目录下创建一个resources/目录,并将静态内容放在那里。他们将被联系到:http://localhost:3000/koo.htm。(假设server.port为3000)
您可以使用spring.resources.static-locations在application.properties中自定义这些目录。
您可以继续在https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot中阅读
https://stackoverflow.com/questions/70732157
复制相似问题