Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate ReactJS with spring Boot

I want to integrate ReactJS with Spring-boot and maven but I don't know how.

I can use npm to install it but I don't know in which path I will do that.

npm init
npm install --save react react-dom
like image 878
mosab Avatar asked Jul 27 '17 10:07

mosab


2 Answers

See frontend-maven-plugin

You should add something like this to your pom.xml file

<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.2</version>
    <configuration>
        <installDirectory>target</installDirectory>
    </configuration>
    <executions>
        <execution>
            <id>install node and npm</id>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
            <configuration>
                <nodeVersion>v4.4.5</nodeVersion>
                <npmVersion>3.9.2</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>

There should be webpack.config.js and package.json together with pom.xml and the webpack is like this

var path = require('path');
var webpack = require('webpack');
var packageJSON = require('./package.json');

module.exports = {
    entry: [
              'webpack/hot/only-dev-server',
              './src/main/resources/static/App.js'],
    devtool: 'sourcemaps',
    cache: true,
//    debug: true,
    output: {
        path: __dirname,
        filename: './src/main/resources/static/built/bundle.js',
        publicPath: 'http://localhost:8080/yourServletContextHere'
    },
    resolve: {extensions: ['.js', '.jsx']},
    plugins: [
               new webpack.HotModuleReplacementPlugin()
               ,new webpack.LoaderOptionsPlugin({
                     debug: true
                   })
        ],
    module: {
        loaders: [
            {
                test: path.join(__dirname, '.'),
                exclude: /(node_modules)/,
                loader: 'babel-loader',
                query: {
                    cacheDirectory: true,
                    presets: ['es2015', 'react']
                }
            },

        ]
    },
    devServer: {
            noInfo: false,
            quiet: false,
            lazy: false,
            watchOptions: {
                poll: true
           }
        }
};
like image 58
StanislavL Avatar answered Oct 19 '22 08:10

StanislavL


Have a look through this repository it's from spring. The projects here uses React+SpringBoot+WebpackV1

https://github.com/spring-guides/tut-react-and-spring-data-rest

Here is the actual tutorial for the above repository, have a read through its very well explained. https://spring.io/guides/tutorials/react-and-spring-data-rest/

like image 30
Sam Kaz Avatar answered Oct 19 '22 06:10

Sam Kaz