react + spring引导开发设置

时间:2019-08-22 13:47:56

标签: reactjs spring-boot

我正在尝试在开发模式下设置React和Spring Boot。我有运行前端和后端单独的端口,即; http://localhost:3000/api/hellohttp://localhost:8080/api/hello。我喜欢在后端调用服务,并在前端显示结果,为此,我在react app package.json文件中添加了“ proxy”:“ http://localhost:8080”。但是它不起作用。

请帮助我解决问题?

package.json文件

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.3.1",
    "react-dom": "^16.3.1",
    "react-scripts": "1.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:8080",
}

我可以显示带有前端reactjs代码的后端spring boot代码

1 个答案:

答案 0 :(得分:0)

您不必定义代理端口。 只需运行您的spring boot项目,然后从react应用程序发送请求

例如在React中:

rasS

春季靴:

        fetch("http://localhost:portNumber/login", {
        method: 'POST',
        headers: {'content-type': 'application/json'},
        body: JSON.stringify({
        email: this.state.emailAddress,
        password: this.state.password})
})
        .then(resopnse => {
    if(response.status === 200){
    }
        })