1

如果控制器中有具有合适路径模板的方法,则 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弹簧启动版本。

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类似的请求/andrewreadersBooks方法将工作三次。先为/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]

这个答案帮助了我。我将以下行添加到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

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

更新

我在本指南中的项目中添加了 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

并且只有在授权之后,如果添加了弹簧安全。

看起来控制器优先于资源。即使在网络安全之后!但如果我正确理解克雷格·沃尔斯,它应该正好相反。否则,无法获取资源,具有请求模式/{reader}

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

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

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

更新

正如@PiotrP.Karwasz 建议的那样,我尝试按照此处所示更改优先级。它有后果,但没有解决问题。我创建了一个没有弹簧安全的新项目。现在我的项目结构是这样的:

.
├── 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。反正class里面的方法requestMappingHandlerMappingWebMvcConfigurationSupport一行

mapping.setOrder(0);

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

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

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

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

4

0 回答 0