Spring Download / Angular 4 - Routing in the application hits the server

I have an Angular 4 (ES6) application that I want to serve from a Spring Boot application. My Angular application has an index.html file, and when I access http: // localhost: 8080, Spring Boot knows how to map it to the index.html file, which in Angular is mapped to "/ search".

However, I have another route called "adminlogin" that I would like to get through

HTTP: // local: 8080 / adminLogin

But in this case, it gets into my Spring Boot application, which has no mapping, and then throws an error.

How can I get my address http: // localhost: 8080 / adminLogin to go to my Angular app?

+7
source share
2 answers

I had a similar problem with my SpringBoot 2 and Angular6 application. I implemented an interface WebMvcConfigurerto override addResourceHandlers()and redirect to index.htmlwhen addResourceHandlers()no mappings were found in the controllers . this can be done in Spring boot 1.5.x, extending the (now obsolete) class WebMvcConfigurerAdaptor. this is discussed in detail in this stackoverflow: fooobar.com/questions/1017173 / ...

I put a built-in corner application in this place target/classes/staticusing the outputPathfield in angular.json(earlier .angular-cli.json). Here is a sample code:

@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");
                }
            });
    }
}
+6
source

adminlogin controller /error, .

angular, adminlogin Angular.

, .

@Controller
public class RouteToAngular implements ErrorController {

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

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

: 404 Angular.

, .

0

Source: https://habr.com/ru/post/1685425/


All Articles