728x90
설치하기
Spring Boot + React.js 개발환경 연동하기
Spring Boot와 React.js를 연동해 개발환경을 만들고, 빌드해서 jar 파일로까지 만들어보는 과정입니다.
velog.io
이분 벨로그 참고해서 주소 띄우는건 성공했는데
리액트앱 열었을때 백엔드 데이터가 넘어오질 않아서
해결한 방법임
import React, {useEffect, useState} from 'react';
import axios from 'axios';
function App() {
const [hello, setHello] = useState('')
useEffect(() => {
axios.get('http://localhost:8080/api/hello')
.then(response => setHello(response.data))
.catch(error => console.log(error))
}, []);
return (
<div>
백엔드에서 가져온 데이터입니다 : {hello}
</div>
);
}
export default App;
주소는 백엔드 주소랑 동일하게 다 써줘야한다
그리고 컨트롤러에도
package SeAH.savg;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin
public class HelloWorldController {
@GetMapping("/api/hello")
public String test() {
return "Hello, world!";
}
}
@CrossOrigin 추가 해주면
잘 넘어오게 되어 있음
'React' 카테고리의 다른 글
React 프로젝트 파이어 베이스로 배포하기 (1) | 2024.02.01 |
---|---|
React 마크다운 에디터 구현하기 (2) | 2024.01.09 |
React Email JS로 메일 보내기 (0) | 2024.01.08 |
리액트 스프링부트 연동해서 메일 보내기 (0) | 2023.07.14 |
테일윈드 css 설치하고 사용하기 (0) | 2023.07.09 |