Spring Boot索引未加载

5
我有一个Spring Boot应用程序。 我没有使用@EnableWebMvc,并且我的资源存放在src/main/resources/static文件夹中。当我尝试加载localhost:8080/ui/时,它只是下载一个随机文件(类型:octet-stream)。如果我直接使用/ui/index.html,它可以正常工作。 我还使用了WebSecurityConfigurerAdapter,但这似乎不是问题的原因。 有人遇到过这种情况吗?我希望在请求localhost:8080/ui/时加载index.html文件。

What I see when I load /ui/

我如何启动我的应用程序:
package my.package;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

//@EnableWebMvc //Breaks js resource loading
@SpringBootApplication
@EnableJpaRepositories
@EnableZuulProxy
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

我的pom.xml文件
<?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>nl.jaarsma</groupId>
    <artifactId>my-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>my-application</name>
    <description>Example app</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.springframework.boot</groupId> -->
<!--            <artifactId>spring-boot-starter-actuator</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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <!-- <scope>runtime</scope> -->
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.6</version>
        </dependency>


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

<!--        <dependency> -->
<!--            <groupId>org.springframework.boot</groupId> -->
<!--            <artifactId>spring-boot-starter-test</artifactId> -->
<!--            <scope>test</scope> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.springframework.security</groupId> -->
<!--            <artifactId>spring-security-test</artifactId> -->
<!--            <scope>test</scope> -->
<!--        </dependency> -->

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


    <profiles>
        <profile>
            <id>include-ui</id>
            <build>
            <plugins>
                <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>

                    <configuration>
                        <workingDirectory>ui</workingDirectory>
                    </configuration>
                    <executions>
                        <execution>
                            <id>install node and npm</id>
                            <goals>
                                <goal>install-node-and-npm</goal>
                            </goals>
                            <configuration>
                                <nodeVersion>v6.11.1</nodeVersion>
                                <npmVersion>3.10.10</npmVersion>
                            </configuration>
                        </execution>

                        <execution>
                            <id>npm install</id>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <configuration>
                                <arguments>install</arguments>
                            </configuration>
                        </execution>

                        <execution>
                            <id>npm run build</id>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <configuration>
                                <arguments>run build</arguments>
                            </configuration>
                        </execution>

                    </executions>
                </plugin>
            </plugins>

                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                    <resource>
                        <filtering>false</filtering>
                        <directory>ui/dist</directory>
                        <includes>
                            <include>**</include>
                        </includes>
                        <targetPath>static/ui</targetPath>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>
</project>

@Vasan 因为它似乎也会破坏资源加载。也许使用WebMVC有解决方案可行? - Jelte
很遗憾,那不起作用。 - Jelte
没有看到你的源代码,我们怎么知道问题出在哪里? - eis
@eis 我使用一个Maven插件将我的UI资源复制到“/static/ui”目录下。如果我打开我的JAR文件,我可以看到我的资源确实在“BOOT-INF\classes\static\ui\”文件夹中。 - Jelte
请尝试使用addViewControllers - TraxX
显示剩余7条评论
4个回答

5
Spring Boot会自动添加位于以下任何目录中的静态Web资源:链接
/META-INF/resources/
/resources/
/static/
/public/

默认情况下,Spring Boot从类路径中的资源"/static"(或"/public")提供静态内容。

index.html资源是特殊的,因为如果存在,它将用作“欢迎页面”,这意味着它将作为根资源提供,即在我们的示例中的http://localhost:8080/处。 链接

对于您的情况,您需要告诉Spring index.html的位置

public class MyApplication {


    @Bean
    WebMvcConfigurer configurer () {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addResourceHandlers (ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/ui/").
                          addResourceLocations("classpath:/static/ui/index.html");
            }
        };
    }


    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

谢谢您的回答,不幸的是当我尝试加载/ui/时,它给了我一个404错误。奇怪的是,如果我将我的UI部署在根目录:/而不是/ui/,它就可以正常工作。 - Jelte

2

由于您将静态资源放在“/static/ui”中,因此您需要通知Spring Boot。

@Configuration
public class WebMvcConfig  extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.setOrder(-1)
        .addResourceHandler("/index.html")
        .addResourceLocations("classpath:/static/ui/");
    super.addResourceHandlers(registry);
}

如果在 /static/ui 目录下还有其他文件,您可以使用通配符 /**。有关更多详细信息,请参见 Spring 博客:https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot

个人而言,如果确实有真正的业务原因不使用默认设置,我会让构建过程将文件放在/static中,以避免麻烦。 - Jean Marois
谢谢你的回答。不幸的是,这个方法也没有起作用(404)。我还尝试将“/index.html”更改为“/ui/index.html”。目前,我已经按照你在评论中提到的方式,直接将我的静态资源放在了资源文件夹中,解决了这个问题。 - Jelte

1
为什么不使用一个控制器将/ui映射到index.html呢?
@Controller
public class HomeController {

    @RequestMapping("/ui")
    public String home(Model model) {
         return "index.html";
    }
}

此外,您可能需要检查this链接以在不使用@EnableWebMVC的情况下启用它。


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接