i would like to integrate an Angular 4 client app with a Java Spring application working on http://localhost:8080/
and offering some Rest endpoints.
My goal is to be able to call the Angular app from a url like http://localhost:8080/adminisitration
. How can I do that?
Thanks in advance,
as i understand your question just create new file named
proxy.config.json
and paste below code in that file, place file next to.angular-cli.json
for hitting url to backend server don't use
http://localhost:8080/administration
instead use/administration
as we usehttp://localhost:8080/
in our proxy file. inapp.component.ts
file place below code inngOnInit()
start backend server: (tomcat on port
8080
) andstart frontend server:
ng serve --proxy-config proxy.config.json
open browser and type urlhttp://localhost:4200
you will see logs on server and client if any.NOTE: above ports are default as provided by spring boot and angular 4
I think best way is to separate angular 4 app and java spring app.
In my case java spring app is API handling all requests from angular 4 app via proxy (angular-cli proxy -> easy to configure).
Angular 4 app on node.js, developed in visual studio code, and java spring on embedded tomcat (undertow) developed in eclipse. They can be on separated servers (eg. my angular 4 app is on localhost:4200 while java spring API is on http://mydomain.ch:8900)
If you need more info then add comment.
Hope in helps
PS. proxy is handled on client side (angular 4 app) not on server side (java spring)
Take a look at this tutorial might help you link
You can also use angular cli in this tutorial and point it to a resource folder (the one you create... i.e. front end) then set up that as a resource in your maven pom.xml.
The easiest way to serve an angular front-end from a spring-boot application , is to have a multi-module project. Then automate the build process to copy the dist folder from the ui module into the service module during
maven clean install
itself.This way, you could have a single executable jar that serves the angular as well.For instance, consider the following project structure :In this case, you will have three different pom files like as follows.
SampleMultiModuleProject main
pom.xml
: (where all main dependencies are present)//add rest of the dependencies here.
SampleMultiModuleService service
pom.xml
: (for service module and add the springboot maven plugin to make it executable with embedded tomcat, and add other dependencies that are needed in service module , for instance lombok)and finally configure the ui module to build angular like SampleMultiModuleUI
pom.xml
:So what happens is when you do maven clean install, it will trigger the build of the ui module which in turn uses the frontend builder to install a local npm which runs the command specified in the arguments. The
package.json
file in your angular application will by default contain something like :So you are essentially calling this
ng build --prod
through this process.Also inangular.json
set theoutput path
as the resources folder under the service module in your project, so that the assets will be created there.There are two ways first is that you serve angular app from your spring boot application as static resources so you need to pack it into jar and that's not easy when you have two diffrent reposetories for frontend and backend and doesn't look to good from maintenance point of view.
Second is that you have angular static resources on nginx and spring boot app is reachable to angular thru reverse proxy configured on nginx like
location /api/ { proxy_pass http://localhost:8080/api/; }
so when angular asks for
GET http://localhost/api/somerest
it forwards it toGET http://localhost:8080/api/somerest
You would need to prod build your ng app and place that in spring-boot folder
This will help you run your angular-app under spring-boot.
then hit http://localhost:8080/adminisitration, it should work fine