Home > OS >  How do I serve the NextJs application from spring boot application?
How do I serve the NextJs application from spring boot application?

Time:03-07

I have a spring boot rest API service that is running in tomcat.

I have a NextJS UI app running on the node js server.

Now I want my NextJs app to be served by the tomcat server.

What are the necessary steps I need to do for my nextjs app to be served by spring boot static folder?

CodePudding user response:

Springboot has a well written example on how to use React with Spring-Data-Rest and the mvn plugin to use to compile/build your front-end.

I know ReactJs is not NextJS, but it could be of help and could provide you with a a start:

The maven plugin is: frontend-maven-plugin

<build>
    <pluginManagement>
        <plugins>
            <!-- tag::frontend-maven-plugin[] -->
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.9.1</version>
                <configuration>
                    <installDirectory>target</installDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>install node and npm</id>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                        <configuration>
                            <nodeVersion>v12.14.0</nodeVersion>
                            <npmVersion>6.13.4</npmVersion>
                        </configuration>
                    </execution>
                    <execution>
                        <id>npm install</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>install</arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>webpack build</id>
                        <goals>
                            <goal>webpack</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- end::frontend-maven-plugin[] -->
        </plugins>
    </pluginManagement>
</build>

https://spring.io/guides/tutorials/react-and-spring-data-rest/

CodePudding user response:

I found the answer.

Step -1 : Edit package.json -> add "export":" next export" in the scripts property

Step - 2: The above step will build a folder named "out". Copy the contents of out folder. Paste it in the src -> main -> resources -> static folder.

Step -3: Now build the spring boot jar/war, then run it. It will serve the contents inside the static folder.

  • Related