Spring Boot 不提供静态资源

时间:2021-05-11 15:55:39

标签: spring spring-boot spring-mvc tomcat servlet-mapping

如果控制器中存在具有合适路径模板的方法,则 Spring Boot 不会提供静态资源。 我目前正在阅读“Spring Boot in Action”(2015 年)一书。第二章有示例工程。

项目结构如下:

.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── haven
    │   │       ├── Book.java
    │   │       ├── ReadingListApplication.java
    │   │       ├── ReadingListController.java
    │   │       └── ReadingListRepository.java
    │   └── resources
    │       ├── application.properties
    │       ├── static
    │       │   └── style.css
    │       └── templates
    │           └── readingList.html
    └── test
        └── java
            └── haven
                └── ReadingListApplicationTests.java

并使用以下依赖项:

org.springframework.boot:spring-boot-starter-data-jpa
org.springframework.boot:spring-boot-starter-thymeleaf
org.springframework.boot:spring-boot-starter-web
org.springframework.boot:spring-boot-starter-test
com.h2database:h2

我使用 2.4.5 spring boot 版本。

src/main/resources/templates/readingList.html 标头:

...
<head>
   <title>Reading List</title>
   <link rel="stylesheet" th:href="@{/style.css}"></link>
</head>
...

src/main/java/haven/ReadingListController.java :

package haven;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/")
public class ReadingListController {
    private ReadingListRepository readingListRepository;

    @Autowired
    public ReadingListController(ReadingListRepository readingListRepository) {
        this.readingListRepository = readingListRepository;
    }
    
    @RequestMapping(value = "/{reader}", method = RequestMethod.GET)
    public String readersBooks(@PathVariable(value = "reader") String reader,
                               Model model) {
        
        List<Book> readingList = readingListRepository.findByReader(reader);
        if(readingList != null) {
            model.addAttribute("books", readingList);   
        }
        System.out.println("reader name is " + reader);
        return "readingList";
    }
    
    @RequestMapping(value = "/{reader}", method = RequestMethod.POST)
    public String addToReaderList(@PathVariable(value = "reader") String reader,
                                  Book book) {
        book.setReader(reader);
        readingListRepository.save(book);
        return "redirect:{reader}";
    }
}

如果我发出 GET 之类的 /andrew 请求,readersBooks 方法将执行三次。首先是 /andrew,然后是 /style.css,然后是 /favicon.ico

如果我改变两个路径,例如这样

@RequestMapping(value = "/reader/{reader}", method = RequestMethod.GET)
@RequestMapping(value = "/reader/{reader}", method = RequestMethod.POST)

或完全删除这两种方法,然后按预期检索资源。 (在这里,我有点惊讶为什么更改 GET 的路径还不够。我开始收到 style.css 和 favicon.icon 的 405 错误。.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]

This 回答对我有帮助。 我在 application.properties 中添加了以下几行:

spring.web.resources.chain.strategy.fixed.enabled=true
spring.web.resources.chain.strategy.fixed.paths=/**
spring.web.resources.chain.strategy.fixed.version=v1

为什么默认属性不够用?我看过几个教程,每个人都说只需将静态资源放在 paths 之一中,一切都会好起来的。本书中的代码适用于作者,但对我来说并不完全相同。也许有什么改变了? 2015 年的书。这三个属性实际上有什么作用?

更新

我像在这个 guide 中那样向项目添加了 spring 安全性。

org.springframework.boot:spring-boot-starter-security

从现在开始,分为三组:

  • //home - 每个人都可以随时使用
  • /login - 用于身份验证,每个人都可以使用,而且总是可以使用
  • /{reader} - 适用于授权用户

添加了两个类:
src/main/java/haven/MvcConfig.java

package haven;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/login").setViewName("login");
    }
}

src/main/java/haven/WebSecurityConfig.java

package haven;

import java.util.function.Function;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
            .and()
                .formLogin().loginPage("/login").permitAll()
            .and()
                .logout().permitAll();
    }
    
    @Bean
    @Override
    protected UserDetailsService userDetailsService() {
        Function<String, String> encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder()::encode;
        UserDetails user = User.withUsername("user")
                               .password("password").passwordEncoder(encoder)
                               .roles("USER")
                               .build();
        
        return new InMemoryUserDetailsManager(user);
    }
}

src/main/resources/templates/home.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
    xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example</title>
    </head>
    <body>
        <h1>Welcome home!</h1>
    </body>
</html>

src/main/resources/templates/login.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example </title>
    </head>
    <body>
        <div th:if="${param.error}">
            Invalid username and password.
        </div>
        <div th:if="${param.logout}">
            You have been logged out.
        </div>
        <form th:action="@{/login}" method="post">
            <div><label> User Name : <input type="text" name="username"/> </label></div>
            <div><label> Password: <input type="password" name="password"/> </label></div>
            <div><input type="submit" value="Sign In"/></div>
        </form>
    </body>
</html>

当我发出请求时,例如 /peter,服务器要求进行身份验证,重定向到 /login。但是它没有显示资源中的页面 login.html,而是将这个 /login 提供给控制器。控制器将页面 readingList.html 提供给读者名为“login”的用户。此外,此页面正在尝试提取资源 style.cssfavicon.ico。但当然,用户仍然没有通过身份验证,Spring Security 又两次重定向到 /login

换句话说,在添加spring security之前,控制台输出是:

reader name is peter
reader name is style.css
reader name is favicon.ico

但现在:

reader name is login
reader name is login
reader name is login

当然我只希望看到

reader name is peter

并且只有在授权后,如果添加了spring security。

看起来控制器优先于资源。即使经过网络安全!但如果我对 Craig Walls 的理解正确的话,应该正好相反。否则,无法获取资源,请求模式为 /{reader}

也许这会有所帮助。
早些时候,当我使用 web.xml(在另一个项目中)时,我在 servlet 映射方面遇到了类似的问题,url 模式为 /*:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

如果没有这种模式,资源会被正常拉取,不需要任何额外的信息。但是如果你添加它,那么像 /style.css/script.js/image.png 这样的资源请求会被发送到控制器进行处理,而不是仅仅返回这个资源。然后我觉得我没明白什么。我有两种方法,使用 <url-pattern>/blablabla/*</url-pattern> 之类的东西(当然没有将资源放在“blablabla”文件夹中)或任何请求的完全匹配(幸运的是,它们的数量有限)。现在又看到了,但是在spring boot项目中...

更新

正如@PiotrP.Karwasz 所建议的,我尝试按照 here 所示更改优先级。它产生了后果,但并没有解决问题。我创建了一个没有 Spring Security 的新项目。现在我的项目结构是这样的:

.
├── pom.xml
└── src
    └── main
        ├── java
        │   └── haven
        │       ├── Book.java
        │       ├── MvcConfig.java
        │       ├── ReadingListApplication.java
        │       ├── ReadingListController.java
        │       └── ReadingListRepository.java
        └── resources
            ├── application.properties
            ├── static
            │   ├── favicon.ico
            │   ├── page.html
            │   └── style.css
            └── templates
                ├── home.html
                └── reading-list.html

src/main/java/haven/MvcConfig.java

package haven;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.resource.ResourceUrlProvider;

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
    
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.setOrder(0);
    }
    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/home").setViewName("home");
        registry.setOrder(1);
    }
    
    @Override
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping(
            ContentNegotiationManager contentNegotiationManager,
            FormattingConversionService conversionService,
            ResourceUrlProvider resourceUrlProvider) {
        RequestMappingHandlerMapping handler = super.requestMappingHandlerMapping(
                                                        contentNegotiationManager,
                                                        conversionService,
                                                        resourceUrlProvider);
        handler.setOrder(2);
        return handler;
    }
}

不再有没有参数的 requestMappingHandlerMapping 方法。我猜测新方法可能以类似的方式工作。也许我错了。

奇怪的是默认优先级是:

ResourceHandlerRegistry: Integer.MAX_VALUE - 1 = Ordered.LOWEST_PRECEDENCE - 1 = 2147483646
ViewControllerRegistry: 1
RequestMappingHandlerMapping: 0

RequestMappingHandlerMapping 没有字段 order,但在超类 AbstractHandlerMapping 中有

private int order = Ordered.LOWEST_PRECEDENCE;  // default: same as non-Ordered

是2147483647。反正requestMappingHandlerMapping类中的WebMvcConfigurationSupport方法有一行

mapping.setOrder(0);

它的优先级高于 ResourceHandlerRegistry
因此,实际上,默认情况下资源的优先级非常低。但不应该反过来吗?
但是,从现在开始我可以获取资源,但任何其他请求将始终返回 404 响应。即使对于 //home。但是,如果我将 ViewControllerRegistry 的优先级设置为大于或等于 ResourceHandlerRegistry,那么 //home 请求也将起作用,但不是所有其他请求。例如:

ResourceHandlerRegistry: 0
ViewControllerRegistry: 0 (or even -1!)
RequestMappingHandlerMapping: 1

如果我将 RequestMappingHandlerMapping 的优先级设置为高于或等于其他人会导致 readersBooks 方法处理任何请求时的原始问题。有一个例外:如果我发出请求 /style.css,标头将包含 Content-Type: text/css;charset=UTF-8,但正文将作为 reading-list.html 的内容。对于任何其他请求,响应是带有 reading-list.htmlContent-Type: text/html;charset=UTF-8 的内容,因为它最初是在更改优先级之前。

我是这方面的完全新手。这个问题有简单的解决方案吗?我几乎完全确定,即使可以以某种方式正确设置优先级并处理请求,就像我现在尝试做的那样,也只能是“治标不治本”。

我已经设置了这样的优先级:

ResourceHandlerRegistry: 0
ViewControllerRegistry: 1
RequestMappingHandlerMapping: 2

带有 logging.level.web=DEBUG 的启动日志:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.5)

2021-05-17 14:54:49.739  INFO 169307 --- [           main] haven.ReadingListApplication             : Starting ReadingListApplication using Java 1.8.0_292 on haven with PID 169307 (/home/vessel/Documents/eclipse-java-workspace/spring-boot-in-action/readinglist/target/classes started by vessel in /home/vessel/Documents/eclipse-java-workspace/spring-boot-in-action/readinglist)
2021-05-17 14:54:49.745  INFO 169307 --- [           main] haven.ReadingListApplication             : No active profile set, falling back to default profiles: default
2021-05-17 14:54:51.162  INFO 169307 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-05-17 14:54:51.278  INFO 169307 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 95 ms. Found 1 JPA repository interfaces.
2021-05-17 14:54:52.463  INFO 169307 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-05-17 14:54:52.483  INFO 169307 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-05-17 14:54:52.483  INFO 169307 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.45]
2021-05-17 14:54:52.622  INFO 169307 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-05-17 14:54:52.622  INFO 169307 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2718 ms
2021-05-17 14:54:52.655 DEBUG 169307 --- [           main] o.s.b.w.s.ServletContextInitializerBeans : Mapping filters: characterEncodingFilter urls=[/*] order=-2147483648
2021-05-17 14:54:52.656 DEBUG 169307 --- [           main] o.s.b.w.s.ServletContextInitializerBeans : Mapping servlets: dispatcherServlet urls=[/]
2021-05-17 14:54:52.986  INFO 169307 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-05-17 14:54:53.231  INFO 169307 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-05-17 14:54:53.324  INFO 169307 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-05-17 14:54:53.436  INFO 169307 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.30.Final
2021-05-17 14:54:53.762  INFO 169307 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-05-17 14:54:53.966  INFO 169307 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2021-05-17 14:54:55.122  INFO 169307 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-05-17 14:54:55.139  INFO 169307 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-05-17 14:54:55.904 DEBUG 169307 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : 4 mappings in 'requestMappingHandlerMapping'
2021-05-17 14:54:55.921 DEBUG 169307 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Patterns [/, /home] in 'viewControllerHandlerMapping'
2021-05-17 14:54:55.961 DEBUG 169307 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Patterns [/**] in 'resourceHandlerMapping'
2021-05-17 14:54:56.001 DEBUG 169307 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2021-05-17 14:54:56.060 DEBUG 169307 --- [           main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice
2021-05-17 14:54:56.135  WARN 169307 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-05-17 14:54:56.483  INFO 169307 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-05-17 14:54:56.510  INFO 169307 --- [           main] haven.ReadingListApplication             : Started ReadingListApplication in 7.783 seconds (JVM running for 8.76)

第一个请求是 /style.css

2021-05-17 14:55:26.594  INFO 169307 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-05-17 14:55:26.594  INFO 169307 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-05-17 14:55:26.595 DEBUG 169307 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Detected StandardServletMultipartResolver
2021-05-17 14:55:26.595 DEBUG 169307 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Detected AcceptHeaderLocaleResolver
2021-05-17 14:55:26.595 DEBUG 169307 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Detected FixedThemeResolver
2021-05-17 14:55:26.597 DEBUG 169307 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@7a65c995
2021-05-17 14:55:26.599 DEBUG 169307 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Detected org.springframework.web.servlet.support.SessionFlashMapManager@4be490da
2021-05-17 14:55:26.600 DEBUG 169307 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2021-05-17 14:55:26.600  INFO 169307 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms
2021-05-17 14:55:26.626 DEBUG 169307 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : GET "/style.css", parameters={}
2021-05-17 14:55:26.642 DEBUG 169307 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [static/]]
2021-05-17 14:55:26.711 DEBUG 169307 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

第二个请求是 /

2021-05-17 14:56:06.057 DEBUG 169307 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : GET "/", parameters={}
2021-05-17 14:56:06.064 DEBUG 169307 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [static/]]
2021-05-17 14:56:06.069 DEBUG 169307 --- [nio-8080-exec-3] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2021-05-17 14:56:06.070 DEBUG 169307 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2021-05-17 14:56:06.085 DEBUG 169307 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2021-05-17 14:56:06.090 DEBUG 169307 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [static/]]
2021-05-17 14:56:06.092 DEBUG 169307 --- [nio-8080-exec-3] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2021-05-17 14:56:06.093 DEBUG 169307 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

第三个请求是:/home

2021-05-17 14:57:42.016 DEBUG 169307 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : GET "/home", parameters={}
2021-05-17 14:57:42.017 DEBUG 169307 --- [nio-8080-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [static/]]
2021-05-17 14:57:42.019 DEBUG 169307 --- [nio-8080-exec-5] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2021-05-17 14:57:42.022 DEBUG 169307 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2021-05-17 14:57:42.023 DEBUG 169307 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2021-05-17 14:57:42.026 DEBUG 169307 --- [nio-8080-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [static/]]
2021-05-17 14:57:42.027 DEBUG 169307 --- [nio-8080-exec-5] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2021-05-17 14:57:42.030 DEBUG 169307 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

第四个请求是 /andrew

2021-05-17 14:58:30.045 DEBUG 169307 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet        : GET "/andrew", parameters={}
2021-05-17 14:58:30.046 DEBUG 169307 --- [nio-8080-exec-8] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [static/]]
2021-05-17 14:58:30.047 DEBUG 169307 --- [nio-8080-exec-8] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2021-05-17 14:58:30.048 DEBUG 169307 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2021-05-17 14:58:30.052 DEBUG 169307 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2021-05-17 14:58:30.053 DEBUG 169307 --- [nio-8080-exec-8] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [static/]]
2021-05-17 14:58:30.054 DEBUG 169307 --- [nio-8080-exec-8] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2021-05-17 14:58:30.059 DEBUG 169307 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

我第二次这样设置优先级:

ResourceHandlerRegistry: 0
ViewControllerRegistry: 0
RequestMappingHandlerMapping: 1

除了对第二个和第三个请求的响应外,没有任何变化。似乎当一个资源具有最高优先级时,它会阻止其他人处理请求。
第二个请求是 /

2021-05-17 15:00:43.815 DEBUG 169868 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : GET "/", parameters={}
2021-05-17 15:00:43.816 DEBUG 169868 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ParameterizableViewController [view="home"]
2021-05-17 15:00:44.345 DEBUG 169868 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

第三个请求是:/home

2021-05-17 15:01:41.909 DEBUG 169868 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : GET "/home", parameters={}
2021-05-17 15:01:41.912 DEBUG 169868 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ParameterizableViewController [view="home"]
2021-05-17 15:01:41.915 DEBUG 169868 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

我需要显示一些其他日志吗?也许最初没有正确配置?

0 个答案:

没有答案