2

This Spring-boot project is integrated with Angular 4. I can reach the home page when type the url adress >> localhost:8080.

I get whitelabel error page when type any diffent url is added parameter like: /login, /signup.

There was an unexpected error (type=Not Found, status=404).

I can reach these pages (localhost:8080/signup, localhost:8080/foo, ..) on website using angular-routing. So the problem is about only hitting url directly.

So How can I solve this, any idea to check would be helpful.

Note: There is no authorization for these url in server side.

Edit: index.html path added.

src/main/resources
  static
    assets
    index.html
    bundle
    bundle
    ..

routing.ts

export const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
    pathMatch: 'full'
  },
  {
    path:'signup',
    component: SignupComponent,
    canActivate: [GuestGuard],
    pathMatch:'full'
  },
krezus
  • 1,281
  • 1
  • 13
  • 29

2 Answers2

1

Create a controller and forward every request to index.html as follows,

@RequestMapping({  "/signup", "/purchase", "/credit"})
public String index() {
    return "forward:/index.html";
}

By doing this, angular will pickup the url and navigate to the corresponding pages. Also please modify the request urls as needed. Whenever a request is hit from browser directly, it will pickup by the spring boot and spring boot doesnt know the angular routing urls. To prevent this we need to forward all request to index.html.

Or you can use the Use HashLocationStrategy as @Vikas suggested

Anto Antony
  • 842
  • 6
  • 12
  • Thanks, I will try this solution – krezus May 23 '18 at 11:11
  • Doing so will override all static resources handling, and browsing to "/does-not-exist" will never respond with an HTTP 404. – Brian Clozel May 23 '18 at 12:00
  • @BrianClozel you are right that is why i mentioned 'Also please modify the request urls as needed.'. We need to put the root urls of the angular application. for example /signUp, /login etc – Anto Antony May 23 '18 at 12:35
  • @AntoAntony I have tried this solution and it didn't work for me. `index.html` is under **resources/static** path. Moreover this solution make every url request with params direct the home page. – krezus May 23 '18 at 14:18
  • @krezus it will work. Please try to add the root paths of your angular projects manually. if you go for /** then it will redirect everything to index.html – Anto Antony May 24 '18 at 12:14
  • @AntoAntony this returns a html page written `forward:/index.html` and I want to reach the signup page from browser directly not want to go home page or index html. – krezus May 24 '18 at 12:19
  • @krezus is this signup page is part of angular application or a spring web application? if its the part of angular, you can forward the requests to index.html and angular will redirect to the signup page – Anto Antony May 24 '18 at 12:28
  • Actually I dont understand clearly. Yes it is part of angular side.Correct me if I misunderstood You said that when I forward or direct these requests (/signup) to index html, angular will do redirect to signup ? If yes how ? what I need to add angular side? @AntoAntony – krezus May 24 '18 at 12:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171697/discussion-between-anto-antony-and-krezus). – Anto Antony May 24 '18 at 12:42
1

I have implemented directly this WebMvcConfigurer and it works perfectly for me. This is the reference link.

import java.io.IOException;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;

@Configuration
public class WebMvcConfig 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");
            }
        });
    }
}
krezus
  • 1,281
  • 1
  • 13
  • 29