Spring Boot 상 vue,react,angular 등 라우터 처리하기

Posted by Albert 917Day 18Hour 41Min 27Sec ago [2022-10-15]

스프링상에서도 Controller에서 request 하는 경로를 받아들이도록 되어있고

만약 vue 나 react 같은 프론트 개발하였다면 모두 경로처리를 하는 route 기능을 갖고 있다

하지만 실제 배포시 build 후 스프링부트로 실행시 

Whitelabel Error Page 404 애러가 뜨면서 경로를 프론트 route로 설정된 경로가 무용지물이 돼버린다.

이를 해결하기위하여 아래와같은 config 파일 

import java.io.IOException;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/tag/*")
.addResourceLocations("classpath:/static/")
.resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String resourcePath,
Resource location) throws IOException {
Resource requestedResource = location.createRelative(resourcePath);
return requestedResource.exists() && requestedResource.isReadable() ? requestedResource
: new ClassPathResource("/static/index.html");
}
});
}
}


https://stackoverflow.com/questions/38516667/springboot-angular2-how-to-handle-html5-urls



































LIST

Copyright © 2014 visionboy.me All Right Reserved.