Search code examples
javaangularmicronaut

Micronaut + Angular : Refresh page


I'm building an application with Micronaut and Angular 4.

I've configured the Micronaut to Serving Static Resources

micronaut:
    router:
        static:
            resources:
                enabled: true
                mapping: /**
                paths: classpath:public

Everything works well (DefaultURL route:http://localhost:8080/dashboard). The Angular app is loaded and the user interacts with the app and navigate through the routes correctly.

In the controller I configured the server to redirect to index.html.If the path does not exist in the server.

@Get("/{[path:[^\\.]*}")
    @Secured(SecurityRule.IS_ANONYMOUS)
    @Produces(MediaType.TEXT_HTML)
    public HttpResponse<?> refresh() {
        return HttpResponse.redirect(URI.create("/index.html"));
    }

But when the user refreshes the page (e.g. pressing F5).

If the current Url is "http://localhost:8080/userdetails/status" after refresh the Angular app goes to the default route "http://localhost:8080/dashboard", instead of the route which the user was in "http://localhost:8080/userdetails/status"

Please help me on this Thanks


Solution

  • Find the Working Code below which I have been implemented.

    @Controller("/")
    public class IndexController {
        @Inject
        ResourceResolver res;
    
    
        @Get("/{[path:[^\\.]*}")
        @Secured(SecurityRule.IS_ANONYMOUS)
        @Produces(MediaType.TEXT_HTML)
        public HttpResponse<?> refresh(HttpRequest<?> request) {
            StreamedFile indexFile = new StreamedFile(res.getResource("classpath:public/index.html").get());
            return HttpResponse.ok(indexFile);
        }
    }