java - 如何在邮递员中为此特定方法传递请求参数?
问题描述
我正在尝试实现从 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);
}
解决方案
创建一个包含所有字段的实体或 DTO,例如。
class User{
int id,
String parameter,
String updateToBe
// getters and setters
}
public void updateUser(@RequestBody User user)
{
service.updateUser(user);
}
从 Postman 发送 JSON 对象
推荐阅读
- java - Android Photo/Media/File Runtime 权限问题
- ibm-cloud - IBM Watson Assistant:如何将 json 响应放入按钮选项中
- django - Django allauth url 从反应调用注册视图表单中获取。我怎样才能将数据发送到 API 端点?
- swift - 如何在地面上放置节点?
- angular - mattlewis92/angular-calendar 的 Angular 日历不会在加载时启动我的数据
- ruby - Ruby 模板缩进问题 - puppet
- swiftui - 每次更改 @State 时防止重排/重绘
- typo3 - 如何防止 tx_news 插件为任意新闻记录呈现单一视图?
- java - 如何从 JPA 中的日期列表中查询?
- android - Android - 我们可以防止通过 ADB Shell 启动活动吗?