Skip to content

Latest commit

ย 

History

History
274 lines (198 loc) ยท 7.27 KB

File metadata and controls

274 lines (198 loc) ยท 7.27 KB

์…‹ํŒ… ์ˆœ์„œ

์œˆ๋„์šฐ์˜ ๊ฒฝ์šฐ mvn์ด ์•ˆ๋จนํž ๊ฒฝ์šฐ .\mvnw ์œผ๋กœ ๋ณ€๊ฒฝํ•˜์…”์„œ ๋Œ๋ฆฌ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ„ฐ๋ฏธ๋„์—์„œ m ์ž…๋ ฅํ›„ tab์„ ๋ˆ„๋ฅด์‹œ๋ฉด ์ž๋™์™„์„ฑ๋ฉ๋‹ˆ๋‹ค.

  1. https://start.spring.io ์œ„ ์‚ฌ์ดํŠธ ๋ฐฉ๋ฌธ ์…‹ํŒ… ํ›„ dependency์— web์ถ”๊ฐ€

  2. pom.xml์„ค์ •

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>no.kantega</groupId>
    <artifactId>spring-and-react</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-and-react</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  1. Controller์„ค์ •

--์ฐธ๊ณ  vscode๋ฅผ ์‚ฌ์šฉ์‹œ auto import๋Š” alt + shift + O์ด๋‹ค.

package no.kantega.springandreact;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class HelloController {
    @GetMapping("/api/hello")
    public String hello() {
        return "Hello, the time at the server is now " + new Date() + "\n";
    }
}
  1. mvn spring-boot:run์‹คํ–‰

  2. ๋ฆฌ์—‘ํŠธ๋ฅผ ์ถ”๊ฐ€์‹œํ‚จ๋‹ค

npx create-react-app frontend

  1. http-proxy-middlewar๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. -๊ฐœ๋ฐœ์‹œ ํ•„์š”ํ•จ.

๊ธฐ์กด์— spring-boot๋Š” 8080 port๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด react์˜ ๊ฒฝ์šฐ 3000 port๋ฅผ ์‚ฌ์šฉํ•จ. ๊ฐœ๋ฐœ์‹œ 3000 port์—์„œ 8080 port๋กœ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ์—ญํ™œ์„ ํ•  ์˜ˆ์ •

cd frontend

npm install --save http-proxy-middleware

  1. ๋ฆฌ์—‘ํŠธ ์‹คํ–‰

์‹คํ–‰์œ„์น˜๋Š” frontend ํด๋” ์•ˆ์ด์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

npm start

์ดˆ๊ธฐ ์‹คํ–‰์‹œ์— ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. proxy์—ฐ๊ฒฐ

frontend/src/ setupProxy.js์ถ”๊ฐ€

๋‚ด์šฉ์€

const proxy = require('http-proxy-middleware')

module.exports = function(app) {
    app.use(proxy('/', { target: 'http://localhost:8080/' }));
}

npm start

์—ฐ๊ฒฐ ํ…Œ์ŠคํŠธ : curl http://localhost:3000/api/hello

์ด์ œ ์—ฐ๊ฒฐ๋ฌ๋Š”์ง€ ํ™•์ธ

frontend/src/App.jsํŒŒ์ผ์„ ์—ด์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •

import React, {Component} from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {

    state = {};

    componentDidMount() {
        setInterval(this.hello, 250);
    }

    hello = () => {
        fetch('/api/hello')
            .then(response => response.text())
            .then(message => {
                this.setState({message: message});
            });
    };

    render() {
        return (
            <div className="App">
                <header className="App-header">
                    <img src={logo} className="App-logo" alt="logo"/>
                    <h1 className="App-title">{this.state.message}</h1>
                </header>
                <p className="App-intro">
                    To get started, edit <code>src/App.js</code> and save to reload.
                </p>
            </div>
        );
    }
}

export default App;

ํ™”๋ฉด์ด ์ œ๋Œ€๋กœ ๋ฐ”๋€Œ๋ฉด ์„ฑ๊ณต

  1. ๋ฐฐํฌ์šฉ ํŒจํ‚ค์ง€ ์ƒ์„ฑ

pom.xml ์•ˆ์— /build/plugins์— ๊ธฐ์กด plugin์•„๋ž˜ ์ถ”๊ฐ€

<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.6</version>
    <configuration>
        <workingDirectory>frontend</workingDirectory>
        <installDirectory>target</installDirectory>
    </configuration>
    <executions>
        <execution>
            <id>install node and npm</id>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
            <configuration>
                <nodeVersion>v8.9.4</nodeVersion>
                <npmVersion>5.6.0</npmVersion>
            </configuration>
        </execution>
        <execution>
            <id>npm install</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>install</arguments>
            </configuration>
        </execution>
        <execution>
            <id>npm run build</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>run build</arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

์ถ”๊ฐ€ํ›„

๊ธฐ์กด mvn spring-boot:run ํ–ˆ๋˜ ํ„ฐ๋ฏธ๋„์ฐฝ์—์„œ ctrl + C ์—ฐํƒ€ํ•˜์—ฌ ์„œ๋ฒ„ ๋‹ค์šดํ›„ ์žฌ๊ฐ€๋™.

mvn clean install

์œ„ ๊ณผ์ •์€ reactํŒŒ์ผ์„ spring boot์— ํฌํ•จ์‹œํ‚ค๊ธฐ๊ธฐ ์œ„ํ•ด ๋นŒ๋“œํ–ˆ์Šต๋‹ˆ๋‹ค.

  1. ๋นŒ๋“œ๋œ ํŒŒ์ผ jarํŒŒ์ผ์— ํฌํ•จํ•˜๊ธฐ

pom.xml ์•ˆ์— /build/plugins์— ๊ธฐ์กด plugin์•„๋ž˜ ์ถ”๊ฐ€

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <configuration>
                <target>
                    <copy todir="${project.build.directory}/classes/public">
                        <fileset dir="${project.basedir}/frontend/build"/>
                    </copy>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

์ถ”๊ฐ€ํ›„

๊ธฐ์กด mvn spring-boot:run ํ–ˆ๋˜ ํ„ฐ๋ฏธ๋„์ฐฝ์—์„œ ctrl + C ์—ฐํƒ€ํ•˜์—ฌ ์„œ๋ฒ„ ๋‹ค์šดํ›„ ์žฌ๊ฐ€๋™.

mvn clean install

์œ„ ๊ณผ์ •์„ ๊ฑธ์น˜๋ฉด projectDir/target/ jarํŒŒ์ผ์ด ์ƒ์„ฑ๋จ.

jarํŒŒ์ผ์„ ์‹คํ–‰ํ•ด๋ด…์‹œ๋‹ค.

cd target/

java -jar .\reactwithspringboot-0.0.1-SNAPSHOT.jar

์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋

ํ™•์ธ์€ localhost:8080