문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
wiki:javascript:react:springboot_react [2021/06/17 16:34] jskim |
wiki:javascript:react:springboot_react [2023/01/13 18:44] (현재) |
||
---|---|---|---|
줄 1: | 줄 1: | ||
====== SpringBoot + React ====== | ====== SpringBoot + React ====== | ||
+ | 각각 생성된 스프링부트와 리액트 프로젝트를 연동하는 과정입니다. | ||
+ | * 스프링부트 IDE : STS4 | ||
+ | * 리액트 IDE : VSCode | ||
- | ===== Ref ===== | ||
- | {{tag> eleven react springboot vscode}} | + | ===== SpringBoot ===== |
+ | > | ||
+ | <code java> | ||
+ | @Getter | ||
+ | public static class User | ||
+ | { | ||
+ | int id; | ||
+ | String username; | ||
+ | String passwd; | ||
+ | String email; | ||
+ | |||
+ | public User(int id, String username, String passwd, String email) | ||
+ | { | ||
+ | this.id = id; | ||
+ | this.username = username; | ||
+ | this.passwd = passwd; | ||
+ | this.email = email; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | <code java> | ||
+ | @RestController | ||
+ | public class HiReactController | ||
+ | { | ||
+ | // 리액트와 연결될 주소 매핑 | ||
+ | @PostMapping("/ | ||
+ | public User user() | ||
+ | { | ||
+ | System.out.println("::: | ||
+ | User user = new User(1, " | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | export default App; | ||
+ | </ | ||
+ | \\ | ||
+ | > 스프링부트 서버 실행 후, 리액트 실행하여 화면 확인 | ||
+ | <code bash> | ||
+ | PS ~\react-app> | ||
+ | </ | ||
+ | |||
+ | ===== Ref ===== | ||
+ | [[https:// | ||
+ | |||
+ | {{tag> eleven react springboot |