java - 在 Spring 中将 ReactJS 前端连接到 REST API
问题描述
基于此页面上的教程:https ://spring.io/guides/tutorials/react-and-spring-data-rest/ 我正在尝试为我的 Spring REST API 后端创建一个前端。我已经对这些示例进行了一些修改以适应我在春季的项目,但不幸的是它现在不起作用。现在,我只是想从主页上的数据库中写出一些记录。将出现一个空白的白页,而不是来自数据库的文本。
在这里您可以看到 curl 打印输出:
C:\Users\Admin>curl http://localhost:8080/getAllDelegations
[{"delegationId":3,"user":{"userId":3,"role":[],"companyName":"PeterCorp","companyAddress":"Wojtyły 12 88-T99 bydgoszcz","companyNip":"11111111111","name":"Jan","lastName":"Kowalski","email":"TEST@gmail.com","password":"1234","status":true,"registrationDate":"2020-03-31T13:29:51.142+0000","delegations":[]},"description":"efa","dateTimeStart":"2020-03-31T15:34:00.134+0000","dateTimeStop":"2020-03-31T15:34:00.134+0000","travelDietAmount":0.0,"breakfastNumber":0,"dinnerNumber":0,"supperNumber":0,"transportType":"AUTO","ticketPrice":0.0,"autoCapacity":true,"km":0,"accomodationPrice":0.0,"otherTicketsPrice":0.0,"otherOutlayDesc":0.0,"otherOutlayPrice":0.0}]
我来自 Controller 类的 java 方法:
@RequestMapping(value = "/getAllDelegations", method = RequestMethod.GET)
@ResponseBody
public List<Delegation> getAllDelegations(){
return delegationService.findAll();
}
DelegationsTest.js - 这个文件应该打印出数据库中的数据:
import React from 'react';
const Delegations = (props) => {
return (
<div>
<center><h1>Delegations List</h1></center>
props.delegations.map((delegation) => (
<div>
<div>
<h5>{delegation.delegationId}</h5>
<h6>{delegation.user.lastName}</h6>
<p>{delegation.user.companyName}</p>
</div>
</div>
))
</div>
)
};
export default Delegations
我的主要 app.js 文件:
const React = require('react');
const ReactDOM = require('react-dom');
import Delegations from './components/DelegationsTest';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
delegations: []
};
}
componentDidMount() {
fetch('http://localhost:8080/getAllDelegations')
.then(res => res.json())
.then((data) => {
this.setState({ delegations: data })
})
.catch(console.log)
}
render() {
return (
<div>
<h1>HelloWorld</h1>
<div>
<Delegations delegations={this.state.delegations} />
</div>
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('react')
)
谁能告诉我我在哪里犯了错误?我会很感激。
解决方案
当您使用 componentDidMount 委托组件时,已呈现并且收到的新道具不会更新组件。
我建议您将委托功能组件转换为类组件,并使用 componentWillReceiveProps 方法在收到道具时更新状态
import React from 'react';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
delegations: props.delegations
};
}
componentWillReceiveProps(nextProps) {
this.setState({
delegations: nextProps.delegations
})
}
render() {
return ( <
div >
<
center > < h1 > Delegations List </h1> </center >
this.state.delegations.map((delegation) => ( <
div >
<
div >
<
h5 > {
delegation.delegationId
} < /h5> <
h6 > {
delegation.user.lastName
} < /h6> <
p > {
delegation.user.companyName
} < /p> <
/div> <
/div>
)) <
/div>
)
};
}
export default Delegations
推荐阅读
- javascript - 我需要返回不和谐的用户标签,他会被提及
- r - 根据名称绘制某些列
- scala - 使用akka-http时的问题,circe
- javascript - 如何在 Create React App 的 jest config 中设置时区?
- verilog - iverilog 错误:赋值语句左值中的语法
- python-3.x - 如何从 Python 中创建的按钮类中关闭 kivy 弹出窗口
- python - 如何在kivy的屏幕之间发送输入?
- reactjs - 如何在 Next-i18next / React i18next 中插入一个 Link 组件来改变文本中的位置
- python - python azure sdk - 克隆服务器
- android - 项目选项卡卡在加载屏幕上 如何查看左侧的项目文件夹?