Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

page refresh is giving 404 in angular5 with springboot application

I have a web service running in springboot on port 8080. when I hit below url it works fine:

http://localhost:8080/app , i redirect the url to below:

http://localhost:8080/myHome/home

now when i refresh the page I am getting 404

below is the content of my index.html:

  <base href="/myHome">

package.json:

"scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.conf.json",
    "build": "ng build --dev --deploy-url=\"/app/\"",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "protractor"
},

I had tried using LocationStrategy mentioned in

Angular 2: 404 error occur when I refresh through the browser , but its not working for me.

like image 490
Anand Avatar asked Dec 04 '22 20:12

Anand


1 Answers

I had the same problem in spring boot 2. I have added resources resolver location as static directory and if not matched with any file then load index.html

@Configuration
public class WebConfig 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");
                    }
                });



}
like image 92
maruf571 Avatar answered Mar 16 '23 20:03

maruf571