Spring Boot/Angular 4 - 应用程序中的路由命中服务器

18

我有一个Angular 4 (ES6)应用程序,希望从Spring Boot应用程序中进行服务。我的Angular应用程序有一个index.html,当访问http://localhost:8080地址时,Spring Boot知道映射到Angular中映射为“/search”的index.html文件。

然而,我还有另一个名为“adminlogin”的路由,我通过以下方式访问:

http://localhost:8080/adminLogin

但在这种情况下,它会命中我的Spring Boot应用程序,而该应用程序没有映射,然后会抛出错误。

我如何使我的http://localhost:8080/adminLogin地址前往我的Angular应用程序?


解决方案可以在这篇文章中找到。https://dev59.com/SmAf5IYBdhLWcg3wQg5t#44850886 - Black Dynamite
4个回答

24

我在我的SpringBoot 2和Angular6应用程序中遇到了类似的问题。我实现了WebMvcConfigurer接口来重写addResourceHandlers()方法,并在Spring控制器中找不到映射时重定向到index.html。在Spring Boot 1.5.x中,可以通过扩展(现在已过时的)WebMvcConfigurerAdaptor类来实现此操作。详细讨论可在此stackoverflow主题中找到:https://dev59.com/MlkT5IYBdhLWcg3wfvmn#46854105 我将构建后的angular应用程序放置在此位置target/classes/static中,使用angular.json字段中的outputPath选项(之前是.angular-cli.json)。 下面是示例代码:

@Configuration
public class MyAppWebMvcConfigurer implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**/*")
            .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");
                }
            });
    }
}

8

由于路径adminlogin在任何Spring controller中都没有映射,因此Spring应用程序将请求分派到/error,因此您会收到错误页面。

您需要将其路由回Angular,以便Angular路由adminlogin页面。

以下是实现此操作的简单路由。

@Controller
public class RouteToAngular implements ErrorController {

    @RequestMapping("/error")
    public String handleError() {
        return "/";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

注意:不要忘记在Angular中实现404页面。
我希望这会有所帮助。

0
在我的情况下,我使用ViewControllerRegistry为每个Angular路由创建了一个简单的视图控制器,重定向到“/”。
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        List<String> urlPatterns = List.of(
                "/login",
                "/app-home",
                "/another_route"
        );

        urlPatterns.forEach(pattern -> registry.addViewController(pattern).setViewName("forward:/"));
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
}

0

如果不这样做,我就无法让根URL正常工作。

@EnableWebMvc
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/")
                .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");
                    }
                });

        registry.addResourceHandler("/**/*")
                .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");
                    }
                });
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接