Spring Boot-Angular - 在地址栏中输入URL会产生404 [英] Spring Boot-Angular - Entering Url in Address Bar results in 404

查看:79
本文介绍了Spring Boot-Angular - 在地址栏中输入URL会产生404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要基础知识的帮助 -
我已经集成了Angular和Spring Boot。
我制作了Angular应用程序的生产版本并将6个文件复制到Spring启动静态资源文件夹中。

Need help on the basics - I have integrated Angular and Spring Boot. I made production build of the Angular app and copied the 6 files in the Spring boot static resource folder.

默认情况下,我点击localhost:8080索引。 html呈现为Spring引导自动将其注册为欢迎页面。

By default when I hit localhost:8080 index.html is rendered as Spring boot Automatically registers it as welcome page.

现在,当我在角度内时,我可以通过ANGULAR ROUTER导航到不同的组件,并且网址也在变化。

Now when i am inside angular i can navigate to different component via ANGULAR ROUTER and the url is also changing.

但是当我复制相同的URL例如 - localhost:8080 / myTask并在url地址栏中输入它时会抛出404资源找不到。
因为它首先击中了Spring控制器,因为它没有映射,所以它失败了。

But when i copy the same URL for example - localhost:8080/myTask and enter it in url address bar it throws 404 resource not found. Because it hits the Spring controller first and since there is no mapping for that it fails.

推荐答案

在课堂上如果你在Spring Boot中扩展了WebMvcConfigurerAdapter,在addViewControllers方法中,你应该做这样的事情

In the class where you have extended WebMvcConfigurerAdapter in Spring Boot, inside addViewControllers method, you should do something like this

@Override
    public void addViewControllers(final ViewControllerRegistry registry) {
        super.addViewControllers(registry);
      registry.addViewController("/myTask").setViewName("forward:/");
 }

转发,所有请求,你可以做注册表.addViewController(/ **)。setViewName(forward:/);

for forwarding, all request, you can do registry.addViewController("/**").setViewName("forward:/");

这篇关于Spring Boot-Angular - 在地址栏中输入URL会产生404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆