문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
wiki:javascript:react:springboot_react [2021/06/17 16:45] jskim |
wiki:javascript:react:springboot_react [2023/01/13 18:44] (현재) |
||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| ====== SpringBoot + React ====== | ====== SpringBoot + React ====== | ||
| - | >스프링부트 프로젝트와 리액트 프로젝트를 | + | 각각 생성된 |
| - | \\ | + | * 스프링부트 IDE : STS4 |
| + | * 리액트 IDE : VSCode | ||
| + | |||
| + | |||
| + | ===== SpringBoot ===== | ||
| + | > | ||
| <code java> | <code java> | ||
| - | // 데이터로 사용할 객체생성 | ||
| @Getter | @Getter | ||
| public static class User | public static class User | ||
| 줄 22: | 줄 27: | ||
| } | } | ||
| </ | </ | ||
| - | \\ | ||
| <code java> | <code java> | ||
| @RestController | @RestController | ||
| public class HiReactController | public class HiReactController | ||
| { | { | ||
| - | // 리액트로부터 요청받을 | + | // 리액트와 연결될 |
| @PostMapping("/ | @PostMapping("/ | ||
| public User user() | public User user() | ||
| 줄 36: | 줄 40: | ||
| return user; | return user; | ||
| } | } | ||
| - | |||
| } | } | ||
| + | </ | ||
| + | \\ | ||
| + | ===== React ===== | ||
| + | > 부트와 통신하기 위해 proxy와 axios 라이브러리 설치 | ||
| + | <code bash> | ||
| + | PS ~\react-app> | ||
| + | PS ~\react-app> | ||
| + | </ | ||
| + | \\ | ||
| + | > 프록시 설정을 위해 src/ | ||
| + | <code javascript> | ||
| + | const {createProxyMiddleware} = require(" | ||
| + | |||
| + | module.exports = function(app){ | ||
| + | app.use( | ||
| + | "/ | ||
| + | , createProxyMiddleware({ | ||
| + | target: " | ||
| + | , changeOrigin: | ||
| + | }) | ||
| + | ); | ||
| + | }; | ||
| + | </ | ||
| + | \\ | ||
| + | > axios가 요청을 보내고, 리턴값을 화면에 띄우기 위해 src/App.js 파일 수정 | ||
| + | <code javascript> | ||
| + | import logo from ' | ||
| + | |||
| + | import React, {useState, useEffect} from " | ||
| + | import ' | ||
| + | import Axios from " | ||
| + | |||
| + | |||
| + | function App() | ||
| + | { | ||
| + | const [user, setUser] = useState("" | ||
| + | useEffect(() => { | ||
| + | Axios.post("/ | ||
| + | if(response.data){ | ||
| + | console.log(" | ||
| + | setUser(response.data); | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | alert(" | ||
| + | } | ||
| + | }); | ||
| + | }, []); | ||
| + | |||
| + | return ( | ||
| + | <div className=" | ||
| + | <header className=" | ||
| + | <img src={logo} className=" | ||
| + | |||
| + | < | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <a className=" | ||
| + | Learn React | ||
| + | </a> | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | ); | ||
| + | } | ||
| + | |||
| + | export default App; | ||
| + | </ | ||
| + | \\ | ||
| + | > 스프링부트 서버 실행 후, 리액트 실행하여 화면 확인 | ||
| + | <code bash> | ||
| + | PS ~\react-app> | ||
| </ | </ | ||
| 줄 43: | 줄 121: | ||
| [[https:// | [[https:// | ||
| - | {{tag> eleven react springboot vscode}} | + | {{tag> eleven react springboot |