首页 > 解决方案 > 如何在邮递员中为此特定方法传递请求参数?

问题描述

我正在尝试实现从 Github 获得的这个 spring 应用程序。我仍在学习过程中,所以这可能是一个基本问题。对于此更新用户方法,我需要在邮递员正文中传递哪些参数?我在邮递员正文中写了 { "id":1,"password":"1234"} 但它向我显示了 400 错误的语法错误。

public void updateUser(int id, String parameter, String update)
    {
        Session session=sessionFactory.openSession();
        Transaction tx=session.beginTransaction();
        //System.out.print(mail);
         List<User_org> userList = userList();
         for(User_org i: userList)
         {
             User_org u= i;
             if(u.getId()==id)
             {
                 if(parameter.equalsIgnoreCase("mail"))
                 {   
                     u.setEmail(update);
                     session.update(u);
                     break;
                 }
                 if(parameter.equalsIgnoreCase("phone_no"))
                 {   
                     u.setPhone_num(Integer.parseInt(update));
                     session.update(u);
                     break;
                 }
                 if(parameter.equalsIgnoreCase("password"))
                 {   
                     u.setPassword(update);
                     session.update(u);
                     break;
                 }
             }
         }
        
        session.flush();
        tx.commit();
        //return id;
    }

在控制器中

public void updateUser(@RequestBody int id,String parameter, String updateToBe)
    {

        service.updateUser(id,parameter, updateToBe);
        
    }
    

标签: javaspring-bootpostman

解决方案


创建一个包含所有字段的实体或 DTO,例如。

class User{
int id,
String parameter, 
String updateToBe

// getters and setters

}


public void updateUser(@RequestBody User user)
    {

        service.updateUser(user);
        
    }

从 Postman 发送 JSON 对象


推荐阅读