Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with URL handling with Spring Boot and Angular 2

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)
];
like image 659
Eric Avatar asked Jul 27 '16 09:07

Eric


People also ask

Can we use Angular with spring boot?

The Angular Application With our demo Spring Boot application up and running, we can now create a simple Angular application capable of consuming the REST controller API.

Is spring boot better than Angular?

"Quick to develop", "Great mvc" and "Powerful" are the key factors why developers consider AngularJS; whereas "Powerful and handy", "Easy setup" and "Java" are the primary reasons why Spring Boot is favored.

How do I run a Spring Boot application in angular?

Once the Spring Boot application has been started, we'll open a command console and type the following command: This will start Angular's live development server and also open the browser at http://localhost:4200. We should see the navigation bar with the buttons for listing existing entities and for adding new ones.

Why do we need Cors in Spring Boot angular?

Therefore, we require CORS only on the development environment, to enable working on an Angular app without the need of rebuilding the whole project. You can find the spring-boot-angular-scaffolding project repository on GitHub.

Should I use the default HTML5 style in my Angular 2 project?

Almost all Angular 2 projects should use the default HTML 5 style. It produces URLs that are easier for users to understand. And it preserves the option to do server-side rendering later. The issue is that when I try to access localhost:8080\dashboard, Spring will look for some controller mapping to this path, which it won't have.

Where does the angular application need to be located?

The angular application must be located besides the spring-boot project as requested by the resource plugin defined in the pom.xml. No, <router-outlet> tag is present in my app.component.html. I need to load by hitting the url in browser and not by clicking on some link.


1 Answers

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!

like image 100
Eric Avatar answered Nov 15 '22 04:11

Eric