๐ ๊ฐ๋ฐ ํ๊ฒฝ
โช IDE : VScode 1.90.0, STS 3.9.18
โช JAVA : JDK 11.0.22
โช Spring : SpringFramework 5.0.2.
โช WAS : Apache Tomcat v9.0.89
์ ์ฒด ํ์ผ์ ๋์ค์ ์๊ฐ์ด ์๊ธฐ๋ฉด ์ ๋ก๋ํฉ๋๋ค.
1. React์์ Spring์ผ๋ก GET(Query String) ๋ฐฉ์์ ํตํ ๋ฐ์ดํฐ ์ ๋ฌ
๐ React : Spring.jsx
import React, { useState } from "react";
import axios from "axios";
const Spring = () => {
const [id, setId] = useState("");
const [pw, setPw] = useState("");
function tryLoginGet() {
axios({
url: `http://localhost:8081/myapp/loginget?id=${id}&pw=${pw}`,
}).then((res) => {
console.log(res);
});
}
return (
<div>
<h1>React Spring ์ฐ๊ฒฐ</h1>
<h3>Login</h3>
ID : <input onChange={(e) => setId(e.target.value)}></input>
<br></br>
PW : <input onChange={(e) => setId(e.target.value)}></input>
<br></br>
<button onClick={tryLoginGet}>๋ก๊ทธ์ธ ์๋ (GET)</button>
</div>
);
};
export default Spring;
๐ Spring : ReactController.java
package com.smhrd.myapp;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ReactController {
@RequestMapping(value="/loginget")
public String loginGet(@RequestParam("id") String id, @RequestParam("pw") String pw) {
System.out.println("React GET ํต์ !");
System.out.println("ID : " + map.get("inputId"));
System.out.println("PW : " + map.get("inputPw"));
return "SUCCESS";
}
}
2. React์์ Spring์ผ๋ก POST ๋ฐฉ์์ ํตํ ๋ฐ์ดํฐ ์ ๋ฌ
๐ React : Spring.jsx
import React, { useState } from "react";
import axios from "axios";
const Spring = () => {
const [id, setId] = useState("");
const [pw, setPw] = useState("");
function tryLoginPost() {
axios({
url: "http://localhost:8081/myapp/loginpost",
method: "post",
data:
inputId: id,
inputPw: pw,
}),
}).then((res) => {
console.log(res);
});
}
return (
<div>
<h1>React Spring ์ฐ๊ฒฐ</h1>
<h3>Login</h3>
ID : <input onChange={(e) => setId(e.target.value)}></input>
<br></br>
PW : <input onChange={(e) => setPw(e.target.value)}></input>
<br></br>
<button onClick={tryLoginPost}>๋ก๊ทธ์ธ ์๋ (POST)</button>
</div>
);
};
export default Spring;
๐ Spring : ReactController.java
package com.smhrd.myapp;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin
public class ReactController {
@RequestMapping(value="/loginpost", method = RequestMethod.POST)
public String loginPost(@RequestBody HashMap<String, Object> map) {
System.out.println("React POST ํต์ ");
System.out.println("ID : " + map.get("inputId"));
System.out.println("PW : " + map.get("inputPw"));
return "SUCCESS";
}
}
3. Spring์์ React๋ก POST ๋ฐฉ์์ ํตํ ๋ฐ์ดํฐ ์ ๋ฌ
Spring์ ReactController์์ ArrayList ๋ฐ์ดํฐ๋ฅผ React ์๋ฒ๋ก ๋ณด๋ด๋ ๋ฐฉ๋ฒ์ผ๋ก React Server์ console์ ํ์ธํ๋ฉด ์ ๋ฌ๋ ๋ฐ์ดํฐ ๊ฐ์ ํ์ธํ ์ ์๋ค.
๐ Spring : ReactController.java
package com.smhrd.myapp;
import java.util.ArrayList;
import java.util.HashMap;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.smhrd.myapp.model.SpringMember;
@RestController
@CrossOrigin
public class ReactController {
@RequestMapping(value="/sendData", method=RequestMethod.POST)
public ArrayList<SpringMember> sendData() {
// SpringMember Class ํ์!!!
String[] mArr = new String[3];
SpringMember m1 = new SpringMember("Kim", "1234", "010-1234-5678", "๊ด์ฃผ");
SpringMember m2 = new SpringMember("Lee", "5678", "010-4567-8901", "์ ์ฃผ");
SpringMember m3 = new SpringMember("Park", "9012", "010-2345-6789", "๋ถ์ฐ");
ArrayList<SpringMember> mArr = new ArrayList<SpringMember>();
mArr.add(m1);
mArr.add(m2);
mArr.add(m3);
return mArr;
}
}
๐ React : Spring.jsx
import React from "react";
import axios from "axios";
const Spring = () => {
function getData() {
axios({
url: "http://localhost:8081/myapp/sendData",
method: "POST",
}).then((res) => {
console.log(res);
});
}
return (
<div>
<h1>React Spring ์ฐ๊ฒฐ</h1>
<button onClick={getData}>Spring์์ Data ๊ฐ์ ธ์ค๊ธฐ (POST)</button>
</div>
);
};
export default Spring;
4. React์ Spring ๋ฐ์ดํฐ ์ฐ๋ ์ ๋ฐ์ํ๋ ์๋ฌ ๋ฐ ํด๊ฒฐ ๋ฐฉ๋ฒ
๐ฅ CORS(Cross-Origin Resource Sharing) ์ ์ฑ ์ผ๋ก ์ธํด ๋ฐ์ํ ์๋ฌ
โ ์๋ฌ ๋ด์ฉ
Access to XMLHttpRequest at 'http://localhost:8081/myapp/loginreact' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
๐จ ํด๊ฒฐ ๋ฐฉ๋ฒ
1. React ํ๋ก์ ํธ์ package.json ํ์ผ์ ์๋์ฒ๋ผ Spring ํ๋ก์ ํธ์ ๊ธฐ๋ณธ ์ฃผ์๋ฅผ ์ฝ์ ํ๋ค.
2. Spring ํ๋ก์ ํธ์ Controller ํ์ผ์์ @RestController ํ๋จ์ '@CrossOrigin' ์ด๋ ธํ ์ด์ ์ ์ถ๊ฐํ๋ค.
๐ฅ Request failed with status code 415
โ ์๋ฌ ๋ด์ฉ
ERROR
Request failed with status code 415 AxiosError: Request failed with status code 415 at settle (http://localhost:3000/static/js/bundle.js:42835:12) at XMLHttpRequest.onloadend (http://localhost:3000/static/js/bundle.js:41501:66) at Axios.request (http://localhost:3000/static/js/bundle.js:41988:41)
โ ์๋ฌ ์์ธ
React์ Spring ๋ฐ์ดํฐ ์ฐ๋ ์์ Java์์ JSON ๊ฐ์ฒด๋ฅผ ์ธ์ํ์ง ๋ชปํด์ ๋ฐ์ํ๋ ์๋ฌ
๐จ ํด๊ฒฐ ๋ฐฉ๋ฒ
Java๊ฐ JSON ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ํด๋น Spring ํ๋ก์ ํธ์ pom.xml ํ์ผ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํ๋ค.
gson์ ์ฌ์ฉํ๋ฉด Java๊ฐ JSON ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋๊ณ ๋ํ Spring์์ React๋ก ArrayList ํ์์ผ๋ก ๋ณด๋ผ ๋ ์๋์ผ๋ก ํค๊ฐ์ด ์ค์ ๋์ด ํธ๋ฆฌํ๋ค. gson์ด ์์ผ๋ฉด ํค๊ฐ์ ์ผ์ผ์ด ๋ถ์ฌํด์ผ ํ๋ค.
<!-- JSON ๊ฐ์ฒด ์ฌ์ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ -->
<!-- React์์ Spring์ผ๋ก POST ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ผ ๋ ๋ฐ์ํ ์๋ฌ ํด๊ฒฐ -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
'Library > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React ๊ฐ๋ฐ ํ๊ฒฝ ์ธํ (1) | 2024.08.18 |
---|---|
[React] Router (0) | 2024.06.10 |
ํฌ์คํ ์ด ์ข์๋ค๋ฉด "์ข์์โค๏ธ" ๋๋ "๊ตฌ๋ ๐๐ป" ํด์ฃผ์ธ์!