如何在请求正文 spring mvc 中发送带有实体对象的额外字段?

How to send extra fields with an entity object in request body spring mvc?

我正在创建一个 spring 启动应用程序,其中有一条 post 路线,其中 post 课程详情。

Course.java

public class course{
  String name;
  String days;
}

现在在向 post 发送 post 请求时,我必须添加一些额外的字段,例如:

sort_order,page_size

我的 Post 映射如下所示:

public course postcourse (@RequestBody course c)
{
}

在上面的函数中,请求正文将是:

{
   "name":"Java",
   "duration":"12"
}

但我希望我的请求是:

{
     "name":"Java",
     "duration":"12",
     "page_size":10,
     "sort_order":"reverse"
}

我不能在实体对象中添加 sort_orderpage_size,因为这不是一个好的做法。

有人可以帮忙吗?谢谢

本地人 class 可以帮助您解决这个问题

public course postcourse (@RequestBody course c) {
    class postCourse extends course {
        int page_size;
        String sort_order;
    }
    course yourCourse = new postCourse();
    …
    
    return yourCourse;
}

希望这项工作。