
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Spring Boot!";
}
}
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:3000");
}
}
import React, { useEffect, useState } from "react";
function App() {
const [message, setMessage] = useState("");
useEffect(() => {
fetch("/api/hello")
.then((response) => response.text())
.then((data) => setMessage(data));
}, []);
return (
<div>
<h1>{message}</h1>
</div>
);
}
export default App;
브라우저에서 http://localhost:3000으로 접속하여 확인
