如何在后端获取从 axios.post 发送的值

how to get the values that were sent from axios.post in the backend

我在前端使用react,在后端使用springboot。我无法检索我在后端使用 axios 发送的数据。下面的第一个代码是我创建 post 并发送 3 个我想在后端使用的对象的前端。第二个代码段是我有 post 映射的后端,但我真的很困惑如何获取我从前端发送的那 3 个对象。此外,用户是我拥有名称、消息和电子邮件的 getter 和设置器的地方,因此我想将来自前端的数据设置到用户中的那些变量中。我对 springboot 有点陌生,但我有一些将数据库连接到 springboot 的经验,但在这种情况下,我不需要使用数据库来存储任何东西。我的总体目标是实现一个有效的联系表,用户可以在其中提交 comments/complaints 有关网页的信息,它会将这些电子邮件定向给我。

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.";
    }
    
}

像这样更改您的方法签名:

...
@PostMapping("/signup-success")
public String signupSuccess(@RequestBody User user) {
...
}

@RequestBody注释告诉 Spring 将传入的 http 请求的主体绑定到您的类型。它将检查您的请求参数键和值,检查您在注释后提供的类型,尝试将参数键与您的用户中的字段匹配class,然后将请求中的值复制到您的用户实例。