【发布时间】:2020-11-01 10:40:54
【问题描述】:
我在前端使用 react,后端使用 springboot。我无法检索在后端使用 axios 发送的数据。下面的第一个代码是我发布并发送我想在后端使用的 3 个对象的前端。第二个代码段是我发布映射的后端,但对于如何获取我从前端发送的这 3 个对象感到非常困惑。此外,用户是我拥有名称、消息和电子邮件的 getter 和 setter 的地方,因此我想将前端的数据设置到 User 中的这些变量中。我对springboot有点陌生,但我在将数据库连接到springboot方面有一些经验,但在这种情况下,我不需要使用数据库来存储任何东西。我的总体目标是实现一个有效的联系表单,用户可以在其中提交有关网页的 cmets/投诉,并将这些电子邮件发送给我。
const info = {
name: "Test"
message: "This is comment for test",
email: "test@test.com
};
axios.post("http://localhost:8080/postgressApp/signup-success", info)
.then(response => {
if(response.data != null) {
this.setState({show:true});
setTimeout(() => this.setState({show:false}), 3000);
window.location.reload();
} else {
this.setState({show:false});
}
});
@RestController
@RequestMapping("/postgressApp")
@CrossOrigin(origins="http://localhost:3000")
public class RegistrationController {
private Logger logger = LoggerFactory.getLogger(RegistrationController.class);
@Autowired
private NotificationService notificationService;
@PostMapping("/signup-success")
public String signupSuccess(){
// create user
User user = new User();
// send a notification
try {
notificationService.sendNotificaitoin(user);
}catch( MailException e ){
// catch error
logger.info("Error Sending Email: " + e.getMessage());
}
return "Thank you for registering with us.";
}
}
【问题讨论】:
标签: javascript reactjs spring-boot spring-boot-maven-plugin