简体   繁体   中英

Best way to returns a group by response in spring

I have an object that returns me

[
  {
    id: 123
    name: 'foo'
    date: 2019-09-08
  },
  {
    id: 124
    name: 'foo2'
    date: 2019-09-08
  },
  {
    id: 125
    name: 'foo3'
    date: 2019-09-09
  },
  {
    id: 126
    name: 'foo4'
    date: 2019-09-09
  }
]

Which is the best way to configure an endpoint that gives me these objects grouped by date using Spring Boot?

How can I set a good endpoint name for this feature?

I'm thinking about something like:

@GetMapping(value = "/somethings")
public List<Something> getSomethings(@RequestParam(value = "groupBy", required = false) String groupBy)

Is it a good approach?

Returning the grouped information in a list would not work. You can use a Map with key as the field that you are using to group the list by and value as a list that belongs to that particular group.

For eg:

Map<Date,List<SomeThing>> 

Hope this helps

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