Spring Download + Angular2 on the same server

I successfully integrated the Angular2 application with the Spring backend to download after this tutorial by putting the compiled JS resources in / ui. Everything works fine, the Angular2 application is accessible through the appname / ui URL.

However, I would like to know if there is a way to tell Spring “pass-through” URLs that the children of the / ui path, as Spring is currently intercepting every targeting request / ui / *, preventing the Angular2 router from moving correctly by resources along the path / ui.

Currently, I only have this mapping in one of my controllers:

@RequestMapping(value = "/ui") public String uiIndex() { return "/ui/index.html"; } 

In this case, the interface is correctly displayed in / ui, but Spring sends me errors and 404s for everything under it , when I access them directly from the browser . Navigating the router inside the Angular2 application works fine.

EDIT

I add the compiled Angular2 resources from target / ui to the static / ui folder with this configuration (my project uses maven build):

  <resource> <directory>${project.basedir}/src/main/resources</directory> <includes> <include>*.properties</include> <include>templates/*.*</include> </includes> </resource> <resource> <directory>target/ui</directory> <targetPath>static/ui</targetPath> </resource> 

As is clear, the only problem is that when I enter the URL in the browser, for example / ui / home / settings, Spring intercepts the request and throws errors. I can happily go to / ui and then to / home / settings in the context of Angular.

+5
source share
3 answers

After some trial mistakes, I was finally able to do what I wanted. Many thanks @EpicPandaforce useful comment and fooobar.com/questions/1017173 / ...

The final solution was to create @RequestMapping in @Controller as follows:

 @RequestMapping(value = "/ui/**/{[path:[^\\.]*}") public String redirectUi() { return "forward:/ui/index.html"; } 
+2
source

You can add ResourceHandlers to configure static content that serves spring loading.

Example

 @Configuration public class StaticResourceConfiguration extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/ui").addResourceLocations("file:/path/to/your/angular/files"); } } 

See https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot https://docs.spring.io/spring-boot/docs/current/reference /html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content

Now, step 5 of the tutorial you talked about has a cleaner way to do this, have you skipped this step? (Src / core / resources / static)

0
source

I think the easiest way to do what you need is to use the Angular hash placement Strategy.

import { LocationStrategy, HashLocationStrategy } from '@angular/common';

{ provide: LocationStrategy, useClass: HashLocationStrategy }

I hope to help.

0
source

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


All Articles