본문 바로가기
개발 공부 기록하기/- Spring

Spring No mapping for GET /css/xxx.css

by soulduse 2023. 8. 15.
반응형

 

 

mustache로 간단한 화면을 구성하였다. 

 
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/css/gallery.css">
    <title>아바타 갤러리</title>
</head>
<body>
<h1>아바타 갤러리</h1>
<ul>
    {{#gallery.images}}
        <li>
            <img src="{{url}}" alt="이미지">
            {{#caption}}<p>{{caption}}</p>{{/caption}}
        </li>
    {{/gallery.images}}
</ul>

문제가 되는 부분이 있었는데 아래 CSS link가 안먹히는게 아닌가?

<link rel="stylesheet" href="/css/gallery.css">

 

아래와 같이 css 파일은 static/css 경로에 잘 들어간것을 확인할 수 있다.

그럼에도 불구하고 페이지에 진입시 서버에서 다음과 같은 에러를 출력하고 있었다.

No mapping for GET /css/gallery.css

원인이 무엇일까 한참헤멨는데 내가 예전에 설정한 WebMvcConfigurer을 상속받아 구현한 로직에 문제가 있었다.

 

문제의 코드이다.

@Configuration
@EnableWebMvc
class MvcConfig : WebMvcConfigurer {

    @Bean
    fun ehCacheManager(): EhCacheManagerFactoryBean = EhCacheManagerFactoryBean().apply {
        setConfigLocation(ClassPathResource("ehcache.xml"))
        setShared(true)
    }

    @Bean
    fun cacheManager(): CacheManager = EhCacheCacheManager(ehCacheManager().`object`!!)

    @Bean
    fun taskExecutor(): AsyncTaskExecutor {
        val executor = ThreadPoolTaskExecutor()
        executor.corePoolSize = 10
        executor.maxPoolSize = 50
        executor.setQueueCapacity(100)
        executor.setThreadNamePrefix("MyTaskExecutor-")
        executor.initialize()
        return executor
    }

    override fun configureAsyncSupport(configurer: AsyncSupportConfigurer) {
        configurer.setTaskExecutor(taskExecutor())
    }
}

 

`@EnableWebMvc`는 Spring MVC의 자동 구성을 사용 중지하며, 이 어노테이션을 활성화하면 스프링 부트에 의해 자동으로 구성되는 모든 리소스 핸들러를 비활성화 한다고 한다. 

따라서, `MvcConfig` 클래스에 `@EnableWebMvc`가 있는 경우, 스프링 부트의 기본 리소스 처리가 비활성화되어 `src/main/resources/static`에 위치한 파일을 수동으로 제공하기 위해 `addResourceHandlers` 메서드를 재정의해줘야 한다.

 

아래 부분을 추가해주었고 리소스 가져오기가 잘 동작함을 확인하였다.

@Configuration
@EnableWebMvc
class MvcConfig : WebMvcConfigurer {

    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/static/")   // 정적 리소스 위치 설정.
    }
    ...
}

 

 

반응형

'개발 공부 기록하기 > - Spring' 카테고리의 다른 글

[Spring] @Transactional 에 관해  (0) 2016.05.18