Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

java - Spring Boot-Angular - Entering Url in Address Bar results in 404

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.

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

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:/");
 }

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

Update Thanks Jonas for the Suggestion. Since WebMvcConfigurerAdapter is deprecated in Spring 5.0, you can implement the above logic by extending WebMvcConfigurer


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...