2

I have a spring boot app serving angular resources in my static [resources/static] folder. I also am using the same project to serve my JSON-REST-API endpoints.

Hence I have defined my REST api under localhost:9090/api/... My Angular2 app-build is served under localhost:9090/ui/... via static resources

Now I want to forward all my ui/** urls to ui/index.html/** How do I do this?

P.S. I have introduced a custom static resource url pattern spring.mvc.static-path-pattern=/ui/** Then all my ui/** request will look to the static/** This way I was able to secure my /api/** and "permitAll" ui/** requests

1 Answer 1

1

This simple configuration will do the trick and will even refresh your # routes in angular 2 if enabled.

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/ui").setViewName("forward:/ui/index.html");
        registry.addViewController("/ui/").setViewName("forward:/ui/index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }


}

Pointers: - @SpringBootApplication should be annotated where the spring boot app is run - Do not have @EnableWebMvc as this breaks all auto configuration done by spring boot - See whether this configuration is under a directory you have marked for
@ComponentScan({"com.foo.config"}) - This implementation is specifically tailored for situations where you have a custom spring.mvc.static-path-pattern defined [ui/]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.