Search code examples
springangularspring-bootuniversal

How enable Angular Universal with a Java/Spring backend?


I have an app, where backend implements on Spring using Spring Boot and front-end side use Angular2. Can somebody help me and explain how to integrate Universal using Java becken. Or show me a good example how I can do that.


Solution

  • how to integrate Universal using Java beckend?

    If you are after Universal's SSR (Server-side Rendering) you cannot do that using Java because universal SSR needs a different server side technology, i.e, nodejs

    But you have a another (better) solution

    Spin off a separate frontend app for the angular then

    1. either route all the API calls through the frontend app (CORS proxy method)
    2. or call your Spring boot webservices directly from angular loaded on client browser.

    Now your spring boot application will be just a (micro)service which will only deal with the data, not the presentation (markup/style etc)

    If you are after universal's pre-rendering (pre- generating static HTML/css/javascript as part of your build) the spring-boot can serve those files (so does any static file serving technologies, nginx apache httpd, AWS S3, GCP GCS to name a few).

    All you need is

    1. Either your CI (github action, circleCI, TravisCI, Jenkins etc) can use nodeJs CI containers and run angular universal pre-render and copy the resultant files to static locations that spring boot will serve. A subsequent java build pack those files in to your jar file

    2. Or if your final artifact is a docker image, you can use multistage docker build. Start from nodeJs base image and generate the static file and then in subsequent steps use the JDK base image and copy the previously generated static files

    Personally, I would prefer frontend as a separate app even if I am after the pre-rendering