简体   繁体   中英

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

I am creating a spring boot app where I have a post route which post the course details.

Course.java

public class course{
  String name;
  String days;
}

Now while sending a post request to post it,I have to add few extra fields like:

sort_order , page_size

My Post Mapping looks like this:

public course postcourse (@RequestBody course c)
{
}

In the above function the request body will be:

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

but I want my request to be:

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

I can't add sort_order and page_size in entity object as its not a good practice.

Can someone help? Thanks

A local class could help you to resolve this

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

Hope this work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM