Spring boot 错误白标签错误页面:在级别1上查询映射时出现意外错误(类型=未找到,状态=404)
在我看来,他面临着一个极其模糊和难以理解的问题。 我在Spring Boot+Kotlin上有一个应用程序。以前,应用程序对Rest控制器有例外;最近需要在响应中提供html,因此添加了一个常规控制器。但是,当将此控制器映射到多个级别时-所有请求(具有多个级别)都会导致错误:Spring boot 错误白标签错误页面:在级别1上查询映射时出现意外错误(类型=未找到,状态=404),spring-boot,spring-mvc,kotlin,Spring Boot,Spring Mvc,Kotlin,在我看来,他面临着一个极其模糊和难以理解的问题。 我在Spring Boot+Kotlin上有一个应用程序。以前,应用程序对Rest控制器有例外;最近需要在响应中提供html,因此添加了一个常规控制器。但是,当将此控制器映射到多个级别时-所有请求(具有多个级别)都会导致错误: <html> <body> <h1> Whitelabel Error Page </h1> <p> This application has no explic
<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>
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
}
}
@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 ...
}
网站安全配置
@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
}
}
@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
这是因为Spring解析静态页面的方式。由于
“/welcome/welcome”
是嵌套的,因此您需要使用正确的资源相对路径或绝对路径
@Controller
@RequestMapping("/welcome")
class WelcomeEndpoint {
@GetMapping("/welcome")
fun signIn(): String {
return "../index.html"
}
}
或
天哪,我尝试过这样的选择,我甚至没有想过,非常感谢!