์ ํ ์์
์๋์ฐ์ ๊ฒฝ์ฐ mvn์ด ์๋จนํ ๊ฒฝ์ฐ .\mvnw ์ผ๋ก ๋ณ๊ฒฝํ์ ์ ๋๋ฆฌ์๋ฉด ๋ฉ๋๋ค. ํฐ๋ฏธ๋์์ m ์ ๋ ฅํ tab์ ๋๋ฅด์๋ฉด ์๋์์ฑ๋ฉ๋๋ค.
-
https://start.spring.io ์ ์ฌ์ดํธ ๋ฐฉ๋ฌธ ์ ํ ํ dependency์ web์ถ๊ฐ
-
pom.xml์ค์
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>no.kantega</groupId>
<artifactId>spring-and-react</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-and-react</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- Controller์ค์
--์ฐธ๊ณ vscode๋ฅผ ์ฌ์ฉ์ auto import๋ alt + shift + O์ด๋ค.
package no.kantega.springandreact;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class HelloController {
@GetMapping("/api/hello")
public String hello() {
return "Hello, the time at the server is now " + new Date() + "\n";
}
}
-
mvn spring-boot:run์คํ
-
๋ฆฌ์ํธ๋ฅผ ์ถ๊ฐ์ํจ๋ค
npx create-react-app frontend
- http-proxy-middlewar๋ฅผ ์ถ๊ฐํ๋ค. -๊ฐ๋ฐ์ ํ์ํจ.
๊ธฐ์กด์ spring-boot๋ 8080 port๋ฅผ ์ฌ์ฉํ๋ค๋ฉด react์ ๊ฒฝ์ฐ 3000 port๋ฅผ ์ฌ์ฉํจ. ๊ฐ๋ฐ์ 3000 port์์ 8080 port๋ก ์ฐ๊ฒฐ์์ผ์ฃผ๋ ์ญํ์ ํ ์์
cd frontend
npm install --save http-proxy-middleware
- ๋ฆฌ์ํธ ์คํ
์คํ์์น๋ frontend ํด๋ ์์ด์ด์ผํฉ๋๋ค.
npm start
์ด๊ธฐ ์คํ์์ ์๊ฐ์ด ๊ฑธ๋ฆด ์ ์์ต๋๋ค.
- proxy์ฐ๊ฒฐ
frontend/src/ setupProxy.js์ถ๊ฐ
๋ด์ฉ์
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
app.use(proxy('/', { target: 'http://localhost:8080/' }));
}
npm start
์ฐ๊ฒฐ ํ ์คํธ : curl http://localhost:3000/api/hello
์ด์ ์ฐ๊ฒฐ๋ฌ๋์ง ํ์ธ
frontend/src/App.jsํ์ผ์ ์ด์ด ๋ค์๊ณผ ๊ฐ์ด ์์
import React, {Component} from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
state = {};
componentDidMount() {
setInterval(this.hello, 250);
}
hello = () => {
fetch('/api/hello')
.then(response => response.text())
.then(message => {
this.setState({message: message});
});
};
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo"/>
<h1 className="App-title">{this.state.message}</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App;
ํ๋ฉด์ด ์ ๋๋ก ๋ฐ๋๋ฉด ์ฑ๊ณต
- ๋ฐฐํฌ์ฉ ํจํค์ง ์์ฑ
pom.xml ์์ /build/plugins์ ๊ธฐ์กด plugin์๋ ์ถ๊ฐ
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<workingDirectory>frontend</workingDirectory>
<installDirectory>target</installDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v8.9.4</nodeVersion>
<npmVersion>5.6.0</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>npm run build</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
</executions>
</plugin>
์ถ๊ฐํ
๊ธฐ์กด mvn spring-boot:run ํ๋ ํฐ๋ฏธ๋์ฐฝ์์ ctrl + C ์ฐํํ์ฌ ์๋ฒ ๋ค์ดํ ์ฌ๊ฐ๋.
mvn clean install
์ ๊ณผ์ ์ reactํ์ผ์ spring boot์ ํฌํจ์ํค๊ธฐ๊ธฐ ์ํด ๋น๋ํ์ต๋๋ค.
- ๋น๋๋ ํ์ผ jarํ์ผ์ ํฌํจํ๊ธฐ
pom.xml ์์ /build/plugins์ ๊ธฐ์กด plugin์๋ ์ถ๊ฐ
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<configuration>
<target>
<copy todir="${project.build.directory}/classes/public">
<fileset dir="${project.basedir}/frontend/build"/>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
์ถ๊ฐํ
๊ธฐ์กด mvn spring-boot:run ํ๋ ํฐ๋ฏธ๋์ฐฝ์์ ctrl + C ์ฐํํ์ฌ ์๋ฒ ๋ค์ดํ ์ฌ๊ฐ๋.
mvn clean install
์ ๊ณผ์ ์ ๊ฑธ์น๋ฉด projectDir/target/ jarํ์ผ์ด ์์ฑ๋จ.
jarํ์ผ์ ์คํํด๋ด ์๋ค.
cd target/
java -jar .\reactwithspringboot-0.0.1-SNAPSHOT.jar
์ด๋ ๊ฒ ๋๋ฉด ๋
ํ์ธ์ localhost:8080