1

I'm building a Java Spring Boot Rest API and a front-end in Angular. The Angular front-end needs to be embedded inside the Spring Boot Java Application. To do this I used frontend-maven-plugin by eirslett.

When I run my application, both the front-end and the back-end are located on the same port. So if I want to go to the homepage I need to go to localhost:8080. If f.e. another page is located at localhost:8080/people-overview and the user enters a wrong URL f.e. 'localhost:8080/people' he will be redirected to a white label error page (by Spring Boot). If f.e. a wrong endpoint 'localhost:8080/persons/11' is called and person with id 11 does not exist, he will also get a white label error page.

If an error occurs, spring boot will always do a call to /error by default. But now my front-end will also be calling this /error endpoint.

I changed this behavior so that when /error is called (no matter if it is front-end or back-end because I don't know how to check this) the user is redirected to the homepage. I did this using a CustomErrorController that extends ErrorController (see link below).

https://www.baeldung.com/spring-boot-custom-error-page

My question is if it is possible to only redirect them to the homepage if the URL was not an endpoint (so a front-end error)? And also show the white label error page when it was a correct endpoint, but not a correct id or the person wasn't found or whatever (so a back-end/API error)? Is this possible? If so, how can I do it? Is there a good tutorial or example I can follow? (I have searched already but can't find someone whit a similar problem or a solution.)

Thanks in advance.

Matti VM
  • 1,074
  • 1
  • 11
  • 18

0 Answers0