错误Whitelabel错误页面:使用级别1上的映射查询时发生意外错误(类型=未找到,状态= 404)

在我看来,这是一个非常模棱两可和难以理解的问题。 我在Spring Boot + Kotlin上有一个应用程序。以前,该应用程序具有Rest控制器的例外;最近需要在响应中提供html,因此添加了常规控制器。但是,将此控制器映射到1个以上级别时-所有请求(超过1个级别)都会导致错误:

<html>
<body>
<h1> Whitelabel Error Page </h1>
<p> This application has no explicit mapping for / error,so you are seeing this as a fallback. </p>
<div> There was an unexpected error (type = Not Found,status = 404). </div>
<div> No message available </div>
</body>
</html>

此外,第一个级别的请求可以正常工作。

这与之完全无关。大量尝试解决此问题(无济于事),但可能遗漏了一些东西。

我应用与可能的问题相关联的设置(如果突然有人尝试提供帮助并且需要其他帮助,请告诉我,我会添加此信息)

控制器

@Controller
@RequestMapping("/welcome")
class WelcomeEndpoint {

    @GetMapping
    fun welcome(): String {
        return "index.html"
    }

    @GetMapping("/welcome")
    fun signIn(): String {
        return "index.html"
    }
}

WebMvcConfig

@Configuration
@EnableWebMvc
class WebMvcConfig : WebMvcConfigurer {

    private final val classpathResourceLocations = arrayOf(
            "classpath:/Meta-INF/resources/","classpath:/resources/","classpath:/static/","classpath:/public/"
    )

    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        if (!registry.hasMappingForPattern("/**")) {
            registry.addResourceHandler("/**")
                    .addResourceLocations(*classpathResourceLocations)
        }
    }

    override fun addViewControllers(registry: ViewControllerRegistry) {
        registry.addViewController("/").setViewName("index.html")
    }

    @Bean
    fun internalResourceViewResolver(): ViewResolver {
        val viewResolver = InternalResourceViewResolver()
        viewResolver.setViewClass(InternalResourceView::class.java)
        return viewResolver
    }

}

WebSecurityConfig

@Configuration
@EnableWebSecurity
class CommonSecurityConfig : WebSecurityConfigurerAdapter() {

    private val permitPatterns = arrayOf(
            "/","/welcome/**","/resources/**","/actuator/health**","/swagger-resources/**","/swagger-ui.html","/v2/api-docs","/webjars/**"
    )

    override fun configure(http: HttpSecurity) {
        super.configure(http)

        http.sessionmanagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .cors().configurationSource(corsConfigurationSource())
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS,"/**").permitAll()
                .antMatchers(*permitPatterns).permitAll()
                .antMatchers("/api/internal/**").hasAuthority("ADMIN")
                .anyRequest().authenticated()
                .and()
                .addFilterAfter(filter(),usernamePasswordauthenticationFilter::class.java)
    }

    // ... some logic after ...
}

因此,如果我沿着路径http://localhost:8080/welcome执行请求,我将获得index.html页面 如果我沿着路径http://localhost:8080/welcome/welcome执行请求-我收到上面的错误

index.html文件位于路径src/main/resources/static/index.html

shaojiaying 回答:错误Whitelabel错误页面:使用级别1上的映射查询时发生意外错误(类型=未找到,状态= 404)

这是因为Spring解析静态页面的方式。
由于"/welcome/welcome"是嵌套的,因此您将需要使用正确的资源相对路径或绝对路径。

@Controller
@RequestMapping("/welcome")
class WelcomeEndpoint {

    @GetMapping("/welcome")
    fun signIn(): String {
        return "../index.html"
    }
}

OR

@Controller
@RequestMapping("/welcome")
class WelcomeEndpoint {

    @GetMapping("/welcome")
    fun signIn(): String {
        return "/index.html"
    }
}
本文链接:https://www.f2er.com/3138766.html

大家都在问