6

I am trying to develop an application with Spring Boot for the back end and Angular 2 for the front end, using maven.

The angular 2 front end is in the src/main/resources/static dir of the project.

When I enter the http://localhost:8080/ URL in my browser, all is fine: I can access the angular 2 front end, and the front end can communicate with the rest api perfectly. My angular 2 routing works fine: when I click on a link on the front end, I go the right page and the browser url bar shows the right things (ie. http://localhost:8080/documents)

But the problem is when I try to directly write the same URL in the browser. Spring take over the front end and says the is no mapping for /documents.

Is there a way to tell spring boot to only "listen" to /api/* URL and to "redirect" all the others to the front end?

Here is my Spring Controller class:

@RestController
@RequestMapping("/api")
public class MyRestController {

    @Autowired
    private DocumentsRepository documentRepository;

    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/documents/list",
            method = RequestMethod.GET,
            produces = "application/json")
    public Iterable<RfDoc> findAllDocuments() {
        return documentRepository.findAll();
    }

}

Here is the main application class:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

Here is my app.route.ts:

import { provideRouter, RouterConfig }  from '@angular/router';
import { DocumentComponent } from './doc.component';  

const routes: RouterConfig = [
    {
        path: '',
        redirectTo: 'documents',
        pathMatch: 'full'
    },
    {
        path: "documents",
        component: DocumentComponent
    }
];

export const appRouterProviders = [
    provideRouter(routes)
];
2
  • There is a way to listen /api/* only . For that you need to have a Filter. Commented Jul 27, 2016 at 12:18
  • How can I do that? I'm pretty new to this stuff... Commented Jul 27, 2016 at 12:43

1 Answer 1

2

Ok, so I found a perfectly fine solution (for me, at least): I change my location for the old AngularJS 1.X way, with the # in the URL (i.e. http://localhost:8080/#/documents ).

To obtain this behaviour, I change my bootstrap like this

import { bootstrap }      from '@angular/platform-browser-dynamic';
import { HTTP_PROVIDERS } from '@angular/http';

import { AppComponent }         from './app.component';
import { appRouterProviders }   from './app.routes';
import { AuthService }          from './auth.service';

bootstrap(AppComponent, [AuthService,
    appRouterProviders,
    HTTP_PROVIDERS,
    { provide: LocationStrategy, useClass: HashLocationStrategy }
]);

Hope this can help somebody!

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

3 Comments

Don't use the uglt hashtag notation ...just keep the HTML5 location strategy and redirect all your angular2's routes to the index.html file. Dont redirect all the routes as you will need them for your end points resources (the rest controllers).
How can I do the redirection?
Eric please create a question and post the link so I can past some code.

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.