* ๋ณธ ๊ฒ์๊ธ์ ๋ค์ ๋งํฌ ๊ฒ์๊ธ ์ฐธ์กฐ๋ฅผ ํตํด ์๋ํ ๊ฒฐ๊ณผ๋ฅผ ๊ณต์ ํ๋ ๊ธ์ ๋๋ค :D
sundries-in-myidea.tistory.com/71
Spring Boot์ React๋ฅผ ์ฐ๋ํ์ฌ ๊ฐ๋ฐํ๊ฒฝ์ ๋ง๋ค์ด๋ณด์
์ด๊ธ์ ์ฝ์ด๋ณด๊ธฐ์ ์!! ํ๋ฒ ๊ณ ๋ คํด๋ณผ ์ ์ ์ ๊ฐ ์ ์ด๋์ต๋๋ค ํ๋ฒ ์ฐธ๊ณ ํด๋ณด๊ณ ํ๋ก์ ํธ๋ฅผ ์งํํด์ฃผ์ธ์! [Java/Spring] - Spring Boot์ React๋ฅผ ํตํ ๊ฐ๋ฐํ๊ฒฝ์ ๊ตฌ์ฑํ ๋ ๊ณ ๋ฏผํด๋ณผ์ Spring Boot์ React
sundries-in-myidea.tistory.com
1. ๊ฐ๋ฐํ๊ฒฝ ์ธํ
- STS 4 ์ค์น.
Spring Tools 4 is the next generation of Spring tooling
Largely rebuilt from scratch, Spring Tools 4 provides world-class support for developing Spring-based enterprise applications, whether you prefer Eclipse, Visual Studio Code, or Theia IDE.
spring.io
- node js ์ค์น. ( react ๋ฐ npm ํจํค์ง ์ฌ์ฉ )
Node.js
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
nodejs.org
* ๋ณธ ๊ธ์์๋ LTS ๋ฒ์ ์ค์น.
2. ํ๋ก์ ํธ ์์ฑ
- Spring Initializer ์์ ํ๋ก์ ํธ ์์ฑ ๋ฐ ๋ค์ด๋ก๋
dependency ์๋ Spring Web ๋ง ์ถ๊ฐ.
* ์ ํ๋ก์ ํธ ๋ค์ด ์, JAVA ์ ์ ํํ ๋ฒ์ ์ด ํ์ฌ ์๋์ฐ์ ํ๊ฒฝ ๋ณ์๋ก ์กํ์์ด์ผ ํจ. ( STS jre ๋ฒ์ ๋ํ ๋์ผํ๊ฒ ๋์ด์ผ ํจ )
- ๋ค์ด๋ก๋๋ฐ์ ํ๋ก์ ํธ๋ฅผ STS ์ ํ๋ก์ ํธ import ํ๊ธฐ.
3. Spring Boot ์ํ ํ์ด์ง ์์ฑ
- Spring Boot ๋ด ํ ์คํธ์ฉ ์ปจํธ๋กค๋ฌ ์์ฑ.
์ดํ, ํด๋น ํ๋ก์ ํธ๋ฅผ ๋ฐ ์ํค๊ณ ๋งคํ๋ ๊ฒฝ๋ก๋ก ์ด๋ํ๋ฉด http://localhost:8080/api/hello ์ ์ ์ ๋ค์๊ณผ ๊ฐ์ด ๋ณด์ฌ์ง.
4. React ์ค์น
node.js ๋ฅผ ์ค์นํ๋ค๋ฉด , cmd ์ฐฝ์ ํตํด workspace ๊ฒฝ๋ก๋ก ๊ฐ์ react ์ค์น.
npx create-react-app {reactํ๋ก์ ํธ๋ช
}
์ดํ, ๋ค์ ๋ช ๋ น์ด๋ฅผ ํตํด run ์ํค๋ฉด http://localhost:3000 ์ผ๋ก ์คํ๋จ.
ํ์ฌ๊น์ง ๊ตฌ์กฐ๋ ๋ค์์ ํํ๋ฅผ ๊ฐ์ง๊ณ ๋ก์ปฌ์์ ๋์ ์ค์. ๊ฐ์ ๋ค๋ฅธ ํฌํธ์์ ์คํ์ค์ด๊ธฐ ๋๋ฌธ์ localhost ์์ ์์ฒญ ์ CORS (cross-origin requests) ์ด์๊ฐ ๋ฐ์ํจ.
ํ์ฌ ๋ชฉํ๋ ํด๋น ๋ ์๋น์ค๋ฅผ ์ฐ๋์ํค๋ ๊ฒ.
CROS ์ด์๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์๋ front ์ชฝ์์ proxy ์ค์ ์ ์ก์์ฃผ์ด์ผ ํ๋ค.
5. Spring Boot ์ React ์ฐ๋ํ๊ธฐ
- React ๋ด package.json ์ ๋ค์ ๊ตฌ๋ฌธ ์ถ๊ฐ.
- backend ์ ์ปจํธ๋กค๋ฌ๋ฅผ ํตํด ๊ฐ์ ๋ฐ์์ค๊ธฐ ์ํ React ์ฝ๋ฉ. ( App.js ๋ด ์์ )
import React, {useState, useEffect} from 'react';
import logo from './logo.svg';
import './App.css';
function App () {
const [message, setMessage] = useState("");
useEffect(() => {
fetch('/api/hello')
.then(response => response.text())
.then(message => {
setMessage(message);
});
},[])
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo"/>
<h1 className="App-title">{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;
6. Spring Boot ์ React ์ฑ์ ํจ๊ป ํจํค์ง ์ฒ๋ฆฌ
- spring boot ํ๋ก์ ํธ pom.xml ๋ด plugins ํ๊ทธ ๋ด ๋ค์ ์ถ๊ฐ
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.11.2</version>
<configuration>
<workingDirectory>worksheetfrontend</workingDirectory>
<installDirectory>target</installDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v14.16.0</nodeVersion>
<npmVersion>6.14.11</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>
* ์ฌ๊ธฐ์ node ์ ๋ฒ์ ๊ณผ npm ์ ๋ฒ์ ์ ๋ณธ์ธ์ด ์ค์นํ ๊ฒ๊ณผ ๋์ผํ๊ฒ ๋ง์ถฐ์ฃผ์ด์ผ ํ๋ค.
๋ฒ์ ํ์ธ์ cmd ์ฐฝ์ ์ด์ด , npm version ์ผ๋ก ํ์ธ๊ฐ๋ฅํ๋ค.
์ดํ, cmd ์ฐฝ์ ์ด์ด ํ๋ก์ ํธ ๊ฒฝ๋ก๋ก ์ด๋ํ ํ, ๋ค์ ๋ช ๋ น์ด ์คํํ์ฌ ํ๋ก์ ํธ ๋น๋.
mvnw clean install
์คํ ํ์ react ํด๋๋ก ๊ฐ๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ด build ํด๋๊ฐ ์์ฑ๋๋ค.
7. Spring Boot JAR ํ์ผ์ React Build ํ์ผ ํฌํจ ์ํคํค
- Spring boot ํ๋ก์ ํธ ๋ด pom.xml ์ plugins ํ๊ทธ ํ์์ ๋ค์์ ์ถ๊ฐํ๋ค.
<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}/{reactํ๋ก์ ํธ๋ช
}/build" />
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
* ์ด ์ํ์์ react ์ฑ ํด๋์ ์์น๋ {springBootํ๋ก์ ํธํด๋}/{reactํ๋ก์ ํธํด๋} ์ด๋ค. ์ฆ, ํ๋ก์ ํธ ํด๋ ํ์์ ํฌํจ๋์ด์๋ค.
์ดํ, cmd ์ฐฝ์ ์ด์ด ํ๋ก์ ํธ ๊ฒฝ๋ก๋ก ์ด๋ํ ํ, ๋ค์ ๋ช ๋ น์ด ์คํํ์ฌ ๋ค์ ํ๋ก์ ํธ ๋น๋.
mvnw clean install
8. ์๋ฃ!
๋ค ์๋ฃ ๋๋ฉด, Spring boot ํ๋ก์ ํธ ๋ด target ์ ์์ฑ๋ jar ํ์ผ์ ์คํํ์ฌ ์ ๋์๊ฐ๋์ง ํ์ธํ๋ค.
cmd ๋ฅผ ํตํ์ฌ ํ๋ก์ ํธ target ํด๋ ์ ๊ทผ ํ, ๋ค์ ๋ช ๋ น์ด ์คํํ์ฌ ์ ์ ์คํ๋๋ ์ง ํ์ธ.
http://localhost:8080 ์์ ์ ์ ์คํ๋๋์ง ํ์ธ!
์ฑ๊ณต!
์ด๋ ๊ฒ ํ๋ฉด, ํ๋ก ํธ๋ react ๋ก ๊ฐ๋ฐํ๊ณ , ๋ฐฑ์๋๋ Spring Boot ๋ก ๊ฐ๋ฐํ๋ฉด ๋ฐ๋ก๋ฐ๋ก ๊ฐ๋ฐํ์ฌ ํ๋์ ์๋น์ค๋ก ์ฐ๋์ด ๊ฐ๋ฅํ๋ค.
'๐React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ธํ๋ฐ:์ํ์ฝ๋ฉ - REACT] 3ํ์ฐจ : ์ปดํฌ๋ํธ ๋ง๋ค๊ธฐ (0) | 2021.03.04 |
---|---|
[์ธํ๋ฐ:์ํ์ฝ๋ฉ - REACT] 2ํ์ฐจ : CSS ์ฝ๋ฉ ~ ๋ฐฐํฌํ๊ธฐ (0) | 2021.02.23 |
[์ธํ๋ฐ:์ํ์ฝ๋ฉ - REACT] 1ํ์ฐจ : ์ฒ์ ~ JS ์ฝ๋ฉํ๊ธฐ (0) | 2021.02.21 |
React ํ์ต ์์. (0) | 2021.02.21 |
๋๊ธ