【问题标题】:Whitelabel Error Page error (Spring Boot + React)Whitelabel Error 页面错误(Spring Boot + React)
【发布时间】:2018-07-22 09:08:25
【问题描述】:

localhost:9000 在浏览器上可以正常打开。如果我从菜单中导航到 localhost:9000/about 之类的链接,它工作正常。

但是如果我直接点击localhost:9000/about而不去localhost:9000,那么本地不会发生任何事情,如果我在服务器上执行相同操作,则会产生以下错误:

Whitelabel 错误页面 此应用程序没有明确的映射 /error,因此您将其视为后备。

2018 年 2 月 12 日星期一 14:09:05 IST 出现意外错误(type=Not 找到,状态=404)。没有可用的消息

请帮忙!!

【问题讨论】:

    标签: reactjs spring-boot


    【解决方案1】:

    发生这种情况是因为在您的应用程序中,您尚未定义应用程序在获取 url 请求 localhost:9000 时应该做什么。 对于每个请求映射,您需要定义要执行的操作。对于 localhost:9000,您必须像对 local:9000/about 所做的那样编写您想要执行的操作。

    @RequestMapping("")
    @ResponseBody
    Function_For_HandlingRequest{}
    

    【讨论】:

      【解决方案2】:

      当你直接点击http://localhost:9090/about 时,SpringBoot 会重定向到/login.html。 所以,将每一页重定向到index.html。

      https://github.com/geowarin/boot-react/blob/master/backend/src/main/java/react/config/SinglePageAppConfig.java

      WebMvcConfigurer 接口,从 Spring 5 开始,包含所有方法的默认实现。结果,抽象适配器 (WebMvcConfigurerAdapter) 类被标记为已弃用。

      package your.package.config;
      
      import org.springframework.context.annotation.Configuration;
      import org.springframework.core.io.ClassPathResource;
      import org.springframework.core.io.Resource;
      import org.springframework.util.StringUtils;
      import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
      import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
      import org.springframework.web.servlet.resource.ResourceResolver;
      import org.springframework.web.servlet.resource.ResourceResolverChain;
      
      import javax.servlet.http.HttpServletRequest;
      import java.io.IOException;
      import java.util.Arrays;
      import java.util.List;
      
      /**
       * Redirects every page to index.html
       * Used to handle the router
       */
      @Configuration
      public class SinglePageAppConfig implements WebMvcConfigurer {
      
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/static/")
            .resourceChain(false)
            .addResolver(new PushStateResourceResolver());
        }
      
        private class PushStateResourceResolver implements ResourceResolver {
          private Resource index = new ClassPathResource("/static/index.html");
          private List<String> handledExtensions = Arrays.asList("html", "js", "json", "csv", "css", "png", "svg", "eot", "ttf", "woff", "appcache", "jpg", "jpeg", "gif", "ico");
          private List<String> ignoredPaths = Arrays.asList("api");
      
          @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 (isIgnored(requestPath)) {
              return null;
            }
            if (isHandled(requestPath)) {
              return locations.stream()
                .map(loc -> createRelative(loc, requestPath))
                .filter(resource -> resource != null && resource.exists())
                .findFirst()
                .orElseGet(null);
            }
            return index;
          }
      
          private Resource createRelative(Resource resource, String relativePath) {
            try {
              return resource.createRelative(relativePath);
            } catch (IOException e) {
              return null;
            }
          }
      
          private boolean isIgnored(String path) {
            return ignoredPaths.contains(path);
          }
      
          private boolean isHandled(String path) {
            String extension = StringUtils.getFilenameExtension(path);
            return handledExtensions.stream().anyMatch(ext -> ext.equals(extension));
          }
        }
      }
      

      【讨论】:

        【解决方案3】:

        在应用中

         @Bean
        public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
            return factory -> {
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/index.html");
                factory.addErrorPages(error404Page);
            };
        }
        

        enter image description here

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
        猜你喜欢
        • 2017-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-10
        • 2018-06-09
        • 2018-04-19
        • 2017-10-06
        相关资源
        最近更新 更多