每个网址路由和子路由的春季单页“ / a / ** => /a/index.html,/a/static/**除外”


问题内容

我正在构建具有在子路由下反应单页应用程序的spring网站,并且我当前的url结构应如下所示

localhost/admin/** => react app
localhost/**       => spring thymeleaf/rest/websocket app for everything else

react app mapping:
localhost/admin/static/**  => static react files
localhost/admin/**         => react index.html for everything else

Example of project resources structure:  
resources/
    admin/         <= my admin react files is here
        index.html
        static/    <= react css, js,  statics
    templates/     <= thymeleaf templates
    static/        <= theamleaf static
    ...

所以我需要index.html为每个URL路由及其子路由转发反应文件。基本上是单页面应用程序,可用于除静态文件以外的所有内容

看起来像一个普通的任务,这是我已经尝试过的一些事情:


项目spring的完整工作演示+ react + gradle如何构建项目以及为什么我不能将react文件放在不同的目录中(例如/ resources /
static):https :
//github.com/varren/spring-react-example


不能用于forward:/admin/index.html/admin/**因为这也会产生递归,因为admin/index.html它也在下面admin/**并且必须以admin/static/**某种方式进行拦截。


无法使用addResourceHandlersWebMvcConfigurerAdapter

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/admin/**")
            .addResourceLocations("classpath:/admin/");
}

index.html仅映射到/admin/index.htmlurl,并且此选项几乎有效,但是仅当您从以下位置访问react应用时
localhost/admin/index.html


问题答案:

现在我正在使用自定义ResourceResolver来解决这个问题

演示:https :
//github.com/varren/spring-react-example

@Configuration
public class BaseWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        ResourceResolver resolver = new AdminResourceResolver();
        registry.addResourceHandler("/admin/**")
                .resourceChain(false)
                .addResolver(resolver);


        registry.addResourceHandler("/admin/")
                .resourceChain(false)
                .addResolver(resolver);
    }


    private class AdminResourceResolver implements ResourceResolver {
        private Resource index = new ClassPathResource("/admin/index.html");

        @Override
        public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
            return resolve(requestPath, locations);
        }

        @Override
        public String resolveUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) {
            Resource resolvedResource = resolve(resourcePath, locations);
            if (resolvedResource == null) {
                return null;
            }
            try {
                return resolvedResource.getURL().toString();
            } catch (IOException e) {
                return resolvedResource.getFilename();
            }
        }

        private Resource resolve(String requestPath, List<? extends Resource> locations) {

            if(requestPath == null) return null;

            if (!requestPath.startsWith("static")) {
                return index;
            }else{
                return new ClassPathResource("/admin/" + requestPath);
            }
        }
    }
}